Download code

Jump to: navigation, search

Back to Insertion_sort_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

insertion_sort.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Insertion_sort_(C)?oldid=19160
14 */
15 
16 #include <stdio.h>
17 
18 /* Sort an array of integers */
19 void insertion_sort(int a[], int length) {
20     int i;
21     for (i=0; i < length; i++) {
22         /* Insert a[i] into the sorted sublist */
23 	int j, value = a[i];
24 	for (j = i - 1; j >= 0; j--) {
25 	    if (a[j] <= value) break;
26 	    a[j + 1] = a[j];
27 	}
28 	a[j + 1] = value;
29     }
30 }
31 
32 
33 int main(void) {
34     unsigned int i;
35     int a[] = {5,1,9,3,2};
36     insertion_sort(a, sizeof(a)/sizeof(*a));
37     /* Print out a */
38     for(i=0; i<sizeof(a)/sizeof(*a); i++) {
39         printf("%d\n", a[i]);
40     }
41     return 0;
42 }


hijacker
hijacker
hijacker
hijacker

build_and_run.sh

 1 #!/bin/bash
 2 # The authors of this work have released all rights to it and placed it
 3 # in the public domain under the Creative Commons CC0 1.0 waiver
 4 # (http://creativecommons.org/publicdomain/zero/1.0/).
 5 # 
 6 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 7 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 8 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 9 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
10 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
11 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
12 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 # 
14 # Retrieved from: http://en.literateprograms.org/Insertion_sort_(C)?oldid=19160
15 
16 gcc -Wall -O2 insertion_sort.c -o insertion_sort
17 ./insertion_sort


hijacker
hijacker
hijacker
hijacker