Created
April 22, 2017 14:43
-
-
Save ArtyomLazyan/ff2675df60d9d111d876f8d738701aeb to your computer and use it in GitHub Desktop.
InsertionSort
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
/* ******** INSERTION SORT O(n^2) ********* */ | |
#include <iostream> | |
int ar[] = { 5, 9, 21, 1, 34, 22, 8, 12, 0, 22, 90, 65 }; | |
int size = sizeof(ar) / sizeof(ar[0]); | |
void insertionSort(int arr[]) | |
{ | |
int j; | |
for (int i = 1; i < size; i++) | |
{ | |
j = i; | |
// j @ndunuma i index@ u while ciklov gnuma et indexic dzax verjic tugelov etuma ete inch vor tvic cacr exav | |
// texerov poxuma | |
// isk iranic dzax gtnvoxner@ arden sort exac en | |
while ((j > 0) && (arr[j] < arr[j - 1])) | |
{ | |
int temp = arr[j]; | |
arr[j] = arr[j - 1]; | |
arr[j - 1] = temp; | |
j--; | |
} | |
} | |
} | |
int main() | |
{ | |
insertionSort(ar); | |
for (int i = 0; i < size; i++) | |
std::cout << ar[i] << " "; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment