Abdulg

Insertion Sort [C]

Oct 7th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void PrintArray(int *Start, int Number);
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     if (argc == 1)
  9.     {
  10.         printf("Please give me a space seperated list of numbers to sort!\n");
  11.         return 1;
  12.     }
  13.     argc--;
  14.  
  15.     int SortMe[1024], i;
  16.     for (i = 0; i < argc - 1; i++)
  17.         SortMe[i] = atoi(argv[i + 1]);
  18.  
  19.     for (i = 1; i < argc; i++)
  20.     {
  21.         int Current = SortMe[i];
  22.         int j;
  23.         for (j = i - 1; j != -1 && SortMe[j] > Current; j--)
  24.             SortMe[j + 1] = SortMe[j];
  25.         SortMe[j + 1] = Current;
  26.  
  27.         PrintArray(SortMe, argc);
  28.     }
  29.     return 0;
  30. }
  31.  
  32. void PrintArray(int *Start, int Number)
  33. {
  34.     int i;
  35.     for (i = 0; i < Number; i++)
  36.         printf("%d, ", *(Start + i));
  37.     printf("\n");
  38. }
  39.  
  40. /*
  41. abdul@computer [~/Programming/InsertionSort]:
  42. gcc InsertionSort.c -g -Wall -o InsertionSort
  43. abdul@computer [~/Programming/InsertionSort]:
  44. ./InsertionSort 4 2 8 5 3 8 9 3
  45. 0, 2, 8, 5, 3, 8, 9, 3,
  46. 0, 2, 8, 5, 3, 8, 9, 3,
  47. 0, 2, 5, 8, 3, 8, 9, 3,
  48. 0, 2, 3, 5, 8, 8, 9, 3,
  49. 0, 2, 3, 5, 8, 8, 9, 3,
  50. 0, 2, 3, 5, 8, 8, 9, 3,
  51. 0, 2, 3, 3, 5, 8, 8, 9,
  52. */
Advertisement
Add Comment
Please, Sign In to add comment