Download code
From LiteratePrograms
Back to Insertion_sort_(C)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
build_and_run.sh
1 #!/bin/bash 2 # Copyright (c) 2010 the authors listed at the following URL, and/or 3 # the authors of referenced articles or incorporated external code: 4 # http://en.literateprograms.org/Insertion_sort_(C)?action=history&offset=20081205204844 5 # 6 # Permission is hereby granted, free of charge, to any person obtaining 7 # a copy of this software and associated documentation files (the 8 # "Software"), to deal in the Software without restriction, including 9 # without limitation the rights to use, copy, modify, merge, publish, 10 # distribute, sublicense, and/or sell copies of the Software, and to 11 # permit persons to whom the Software is furnished to do so, subject to 12 # the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be 15 # included in all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 # 25 # Retrieved from: http://en.literateprograms.org/Insertion_sort_(C)?oldid=15530 26 27 gcc -Wall -O2 insertion_sort.c -o insertion_sort 28 ./insertion_sort 29
insertion_sort.c
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Insertion_sort_(C)?action=history&offset=20081205204844 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Insertion_sort_(C)?oldid=15530 25 */ 26 27 #include <stdio.h> 28 29 30 /* Sort an array of integers */ 31 void insertion_sort(int a[], int length) 32 { 33 int i; 34 for (i=0; i < length; i++) 35 { 36 /* Insert a[i] into the sorted sublist */ 37 int j, v = a[i]; 38 39 for (j = i - 1; j >= 0; j--) 40 { 41 if (a[j] <= v) break; 42 a[j + 1] = a[j]; 43 } 44 a[j + 1] = v; 45 46 } 47 } 48 49 void checkThatArrayIsSorted (int array[], int length) 50 { 51 int i; 52 for (i = 1; i < length; i+=1) 53 { 54 if (array[i-1] > array[i]) 55 { 56 printf("The array is not in sorted order at position %d\n", i-1); 57 } 58 } 59 } 60 61 62 int main(void) 63 { 64 unsigned int i; 65 int a[] = {5,1,9,3,2}; 66 insertion_sort(a, sizeof(a)/sizeof(*a)); 67 /* Print out a */ 68 for(i=0; i<sizeof(a)/sizeof(*a); i++) 69 { 70 printf("%d\n", a[i]); 71 } 72 73 checkThatArrayIsSorted(a, sizeof(a)/sizeof(*a)); 74 75 return 0; 76 } 77 78
