Step by step implementation of Insertion Sort (Eve intake 28 section 2)
Question
Write a program that implements insertion sort and shows the step by step iteration as Output.
sample input: 7, 8, 5, 3, 2
sample output: 7, 8, 5, 3, 2
5, 7, 8, 3, 2
3, 5, 7, 8, 2
2, 3, 5, 7, 8
in progress
6
General
2 months
22 Answers
309 views
4
Answers ( 22 )
#include <stdio.h>
#include<math.h>
int main()
{
int n, array[10000];
printf(“Enter number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d integers: “, n);
int p;
for(p=0;p<n;p++)
{
scanf(“%d”,&array[p]);
}
int i, key, j;
for(i=1;i<n;i++)
{
key=array[i];
j=i-1;
while(j>=0 && array[j]>key)
{
array[j+1]= array[j];
j=j-1;
}
array[j+1]=key;
int x;
printf(“n n “);
for(x=0;x<n;x++) {
printf(” %d “, array[x]);
}
}
printf(“n Sorted list in ascending order: “);
int x;
for(x=0;x<n;x++) {
printf(” %d “, array[x]);
}
return 0;
}
// Id number: 17181203047
#include
#include
int main(){
int n, array[500];
printf(“Enter number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d integers: “, n);
int p;
for(p=0;p<n;p++)
{
scanf("%d",&array[p]);
}
int i, key, j;
for(i=1;i=0 && array[j]>key)
{
array[j+1]= array[j];
j=j-1;
}
array[j+1]=key;
int x;
printf(“n n “);
for(x=0;x<n;x++) {
printf(" %d ", array[x]);
}
}
printf(" n Sorted list in ascending order: ");
int x;
for(x=0;x<n;x++) {
printf(" %d ", array[x]);
}
return 0;
}
// Id number: 17181203047
#include
#include
int main(){
int n, array[500];
printf(“Enter number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d integers: “, n);
int p;
for(p=0;p<n;p++)
{
scanf("%d",&array[p]);
}
int i, key, j;
for(i=1;i=0 && array[j]>key)
{
array[j+1]= array[j];
j=j-1;
}
array[j+1]=key;
int x;
printf(“n n “);
for(x=0;x<n;x++) {
printf(" %d ", array[x]);
}
}
printf(" n Sorted list in ascending order: ");
int x;
for(x=0;x<n;x++) {
printf(" %d ", array[x]);
}
return 0;
}
Sorry it a private answer.
ID: 15163203102
#include<iostream>
using namespace std;
int main(){
int i,j,key,arry[5];
for(int b = 0; b<5; b++){
cout<<“nt Please input number “<<b+1<<“: “;
cin>>arry[b];
}
cout<<“nt The array is now: “<<endl;
for(int b = 0; b<5; b++){
cout<<“t “<<arry[b];
}
int n= sizeof(arry)/sizeof(arry[0]);
for(i=1;i<n;i++){
key=arry[i];
j=i-1;
while(j>=0 && arry[j]>key)
{
arry[j+1]= arry[j];
j=j-1;
}
arry[j+1]=key;
cout<<“nnt After sorting “<<i<<” times the array is :”<<endl;
for(int b = 0; b<5; b++){
cout<<“t “<<arry[b];
}
}
cout<<“nnt After final sorting the array is now: “<<endl;
for(int b = 0; b<5; b++){
cout<<“t “<<arry[b];
}
return 0;
}
//ID:17181203067
#include <stdio.h>
#include<math.h>
int main()
{
int n, array[10000];
printf(“Enter number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d integers: “, n);
int p;
for(p=0;p<n;p++)
{
scanf(“%d”,&array[p]);
}
int i, key, j;
for(i=1;i<n;i++)
{
key=array[i];
j=i-1;
while(j>=0 && array[j]>key)
{
array[j+1]= array[j];
j=j-1;
}
array[j+1]=key;
int x;
printf(“n n “);
for(x=0;x<n;x++) {
printf(” %d “, array[x]);
}
}
printf(“n Sorted list in ascending order: “);
int x;
for(x=0;x<n;x++) {
printf(” %d “, array[x]);
}
return 0;
}
// Id number: 17181203047 //
#include <stdio.h>
#include<math.h>
int main(){
int n, array[500];
printf(“Enter number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d integers: “, n);
int p;
for(p=0;p<n;p++)
{
scanf(“%d”,&array[p]);
}
int i, key, j;
for(i=1;i<n;i++)
{
key=array[i];
j=i-1;
while(j>=0 && array[j]>key)
{
array[j+1]= array[j];
j=j-1;
}
array[j+1]=key;
int x;
printf(“n n “);
for(x=0;x<n;x++) {
printf(” %d “, array[x]);
}
}
printf(” n Sorted list in ascending order: “);
int x;
for(x=0;x<n;x++) {
printf(” %d “, array[x]);
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,key;
int arr[5]={7,8,5,3,2};
cout<<“Sample Input : “;
for(int i=0;i<5;i++){
cout<<arr[i]<<” “;
}
cout<<“n”;
for( i=1;i<5;i++){
key=arr[i];
j=i-1;
while(j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
for(int i=0;i<5;i++){
cout<<arr[i]<<” “;
}
cout<<“n”;
}
cout<<“n”;
cout<<“Sample Output: “;
for(int i=0;i<5;i++){
cout<<arr[i]<<” “;
}
return 0;
}
ID: 17181203063
id-17181203058
#include <stdio.h>
#include <stdbool.h>
#define MAX 7
int intArray[MAX] = {4,6,3,2,1,9,7};
void display() {
int i;
printf(“[“);
for(i = 0;i < MAX;i++) {
printf(“%d “,intArray[i]);
}
printf(“]n”);
}
void insertionSort() {
int
for(i = 1; i < MAX; i++) {
valueToInsert;
int holePosition;
int i;
valueToInsert = intArray[i];
holePosition = i;
while (holePosition > 0 && intArray[holePosition-1] > valueToInsert) {
intArray[holePosition] = intArray[holePosition-1];
holePosition–;
}
if(holePosition != i) {
intArray[holePosition] = valueToInsert;
}
printf(“Step %d#:”,i);
display();
}
}
void main() {
printf(“Input Array: “);
display();
insertionSort();
}
Id:17181203003
#include
#include
#define MAX 7
int intArray[MAX] = {4,6,3,2,1,9,7};
void display() {
int i;
printf(“[“);
for(i = 0;i < MAX;i++) {
printf(“%d “,intArray[i]);
}
printf(“]n”);
}
void insertionSort() {
int
for(i = 1; i 0 && intArray[holePosition-1] > valueToInsert) {
intArray[holePosition] = intArray[holePosition-1];
holePosition–;
}
if(holePosition != i) {
intArray[holePosition] = valueToInsert;
}
printf(“Step %d#:”,i);
display();
}
}
void main() {
printf(“Input Array: “);
display();
insertionSort();
}
ID:17181203065
#include<stdio.h>
#include<math.h>
int main()
{
int ary[]={5,4,3,2,1};
int n = sizeof(ary)/sizeof(ary[0]);
int i,key,j,x;
for(i=1;i<n;i++){
key = ary[i];
j=i-1;
while(j>=0 && ary[j]>key){
ary[j+1] = ary[j];
j=j-1;
}
ary[j+1] = key;
for(x=0;x<n;x++){
printf(“%d “,ary[x]);
}
printf(“n”);
}
int p;
printf(“The final Result is: “);
for(p=0;p<n;p++){
printf(“%d “,ary[p]);
}
printf(“n”);
return 0;
}
ID:17181203065
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << “Enter a positive number: “;
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << ” The reverse of the number is: ” << rev << endl;
if (n == rev)
cout << ” The number is a palindrome.”;
else
cout << ” The number is not a palindrome.”;
return 0;
}
// id: 17181203049 //
#include
using namespace std;
int main()
{
int i,j,key;
int arr[5]={7,8,5,3,2};
cout<<"Sample Input : ";
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
cout<<"n";
for( i=1;i=0 && arr[j]>key){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
cout<<"n";
}
cout<<"n";
cout<<"Sample Output: ";
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
return 0;
}
#include
using namespace std;
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
int i, key, j;
for (i = 1; i = 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
printArray(arr,n);
cout<<endl;
}
cout << endl;
printArray(arr,n);
cout << endl;
return 0;
}
#include
using namespace std;
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
int i, key, j;
for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
printArray(arr,n);
cout<<endl;
}
cout << endl;
printArray(arr,n);
cout << endl;
return 0;
}
#include
using namespace std;
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
int i, key, j;
for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
printArray(arr,n);
cout<<endl;
}
cout << endl;
printArray(arr,n);
cout << endl;
return 0;
}
Sorry it a private answer.
Sorry it a private answer.
Name: Md. Motier Rahman
ID# 17181203074
#include<iostream>
using namespace std;
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << ” “;
cout << endl;
}
void insertionSort(int *array, int size) {
int key, j;
for(int i = 1; i<size; i++) {
key = array[i];
j = i;
while(j > 0 && array[j-1]>key) {
array[j] = array[j-1];
j–;
}
array[j] = key;
}
}
int main() {
int n;
cout << “Enter the number of elements: “;
cin >> n;
int arr[n];
cout << “Enter elements:” << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << “Array before show: “;
display(arr, n);
insertionSort(arr, n);
cout << “Array after show: “;
display(arr, n);
}
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << “Enter a positive number: “;
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << ” The reverse of the number is: ” << rev << endl;
if (n == rev)
cout << ” The number is a palindrome.”;
else
cout << ” The number is not a palindrome.”;
return 0;
}
Md shamol Hossain Biswas
ID 17181203066
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << “Enter a positive number: “;
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << ” The reverse of the number is: ” << rev << endl;
if (n == rev)
cout << ” The number is a palindrome.”;
else
cout << ” The number is not a palindrome.”;
return 0;
}