Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Insertion sort
- // Date : 18.10.19
- #include<iostream>
- using namespace std;
- void insertionSort(int a[], int n){
- for(int i = 1; i < n; i++){
- int temp = a[i];
- int j = i - 1;
- while(j >= 0 && a[j] > temp){
- a[j + 1] = a[j];
- j--;
- }
- a[j + 1] = temp;
- }
- }
- int main(){
- int n;
- cout << "Enter range of array: "; cin >> n;
- int a[n], i;
- cout << "Enter the elements of array: ";
- for(i = 0; i < n; i++){
- cin >> a[i];
- }
- insertionSort(a, n);
- cout << "The array after sorting: ";
- for(i = 0; i < n; i++){
- cout << a[i] << ' ';
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement