Skip to content

Instantly share code, notes, and snippets.

@mahmmoudkinawy
Created April 17, 2021 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahmmoudkinawy/b4916a186a89dfc1c3b013e7b41b61bf to your computer and use it in GitHub Desktop.
Save mahmmoudkinawy/b4916a186a89dfc1c3b013e7b41b61bf to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
//8 4 2 1 3
void Sort(int array[], int length){
for(int i = 1 ; i < length ; i++){
int current = array[i]; //index[1] = 4
int j = i - 1; //1 - 1 = 0
while(j >= 0 && array[j] > current){ //8 > 4
array[j+1] = array[j];
j--;
}
array[j+1] = current;
}
}
void print(int array[], int length){
for(int i = 0 ; i < length ; i++)
cerr<<array[i] <<" ";
cout<<endl;
}
int main(){
int arr[] = {-2,-99,-100,100,0,6,3,5,45};
int length = sizeof(arr) / sizeof(arr[0]);
clog<<"Before sorting : ";
print(arr,length);
Sort(arr,length);
clog<<"After sorting : ";
print(arr,length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment