Created
April 7, 2018 13:24
-
-
Save 5quinque/f6739684d2e4a7993927d6faebe701a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
void print_array(int *array, int array_size); | |
int main(int argc, char **argv) { | |
int *array; | |
int array_size; | |
int i = 0; | |
int left = 0; | |
int tmp; | |
if (argc < 2) { | |
printf("Usage: %s [size of list]\n", argv[0]); | |
return 1; | |
} | |
srand(time(NULL)); | |
array_size = atoi(argv[1]); | |
array = calloc(array_size, sizeof(int)); | |
for (int k = 0; k < array_size; k++) | |
array[k] = rand() % 100; | |
print_array(array, array_size); | |
printf("\n"); | |
while (++i < array_size) { | |
if (array[i] > array[i - 1]) | |
continue; | |
left = i; | |
tmp = array[i]; | |
while (tmp < array[--left]) { | |
array[left + 1] = array[left]; | |
array[left] = tmp; | |
} | |
} | |
print_array(array, array_size); | |
return 0; | |
} | |
void print_array(int *array, int array_size) { | |
for (int i = 0; i < array_size; i++) { | |
printf(" %d ", array[i]); | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment