Created
April 17, 2021 18:31
-
-
Save mahmmoudkinawy/b4916a186a89dfc1c3b013e7b41b61bf to your computer and use it in GitHub Desktop.
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
#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