forked from Asiatik/codezilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update InsertionSort.cpp (Asiatik#299)
- Loading branch information
1 parent
724a3ba
commit 761dc79
Showing
1 changed file
with
43 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,50 @@ | ||
// Insertion sort algorithm implemented in C++ | ||
|
||
#include <iostream> | ||
#include<conio.h> | ||
#include<stdlib.h> | ||
|
||
#define MAX_SIZE 5 | ||
|
||
using namespace std; | ||
|
||
// Insertion sort function | ||
|
||
void insertionSort(int array[],int size){ | ||
int current; | ||
int i,j; | ||
for(i=0;i<size;i++){ | ||
current=array[i]; | ||
for(j=i;j>0 && array[j-1] > current;j--){ | ||
array[j]=array[j-1]; | ||
} | ||
array[j]=current; | ||
} | ||
void insertion(int[]); | ||
|
||
int main() { | ||
int arr_sort[MAX_SIZE], i; | ||
|
||
cout << "Simple C++ Insertion Sort Example - Array and Functions\n"; | ||
cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl; | ||
for (i = 0; i < MAX_SIZE; i++) | ||
cin >> arr_sort[i]; | ||
|
||
cout << "\nYour Data :"; | ||
for (i = 0; i < MAX_SIZE; i++) { | ||
cout << "\t" << arr_sort[i]; | ||
} | ||
|
||
insertion(arr_sort); | ||
getch(); | ||
} | ||
|
||
// Main function to perform sorting | ||
|
||
int main(){ | ||
int i; | ||
int array_size; | ||
cout<< "Enter the size of the array to be sorted: "; | ||
cin>> array_size; | ||
int array[array_size]; | ||
cout<<"Enter the elements of the array to be sorted: "; | ||
for(i=0;i<array_size;i++){ | ||
cin>>array[i]; | ||
void insertion(int fn_arr[]) { | ||
int i, j, a, t; | ||
for (i = 1; i < MAX_SIZE; i++) { | ||
t = fn_arr[i]; | ||
j = i - 1; | ||
|
||
while (j >= 0 && fn_arr[j] > t) { | ||
fn_arr[j + 1] = fn_arr[j]; | ||
j = j - 1; | ||
} | ||
fn_arr[j + 1] = t; | ||
|
||
cout << "\nIteration : " << i; | ||
for (a = 0; a < MAX_SIZE; a++) { | ||
cout << "\t" << fn_arr[a]; | ||
} | ||
} | ||
insertionSort(array,array_size); | ||
cout<<"Sorted array is:\n"; | ||
for(i=0;i<array_size;i++){ | ||
cout<<array[i]<<"\n"; | ||
|
||
cout << "\n\nSorted Data :"; | ||
for (i = 0; i < MAX_SIZE; i++) { | ||
cout << "\t" << fn_arr[i]; | ||
} | ||
return 0; | ||
} | ||
} |