Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: Gerardo Cicalese
- *
- * Created on 18 novembre 2019, 14.22
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef int Info;
- void insertionSort(Info *, int);
- void insertInOrder(Info *, int, Info);
- void swap(Info *, Info *);
- int compare(Info, Info);
- int main(void)
- {
- srand(time(NULL));
- Info array[10];
- for(int i=0; i<10; ++i)
- array[i] = rand()%100;
- for(int i=0; i<10; ++i)
- printf("%d\t", array[i]);
- printf("\nSorting...\n");
- insertionSort(array, 10);
- for(int i=0; i<10; ++i)
- printf("%d\t", array[i]);
- return 0;
- }
- void insertionSort(Info * array, int len)
- {
- for(int i = 1; i < len; ++i)
- insertInOrder(array, i, array[i]);
- }
- void insertInOrder(Info * array, int len, Info item)
- {
- int pos = len;
- for(; pos > 0 && 1 == compare(array[pos - 1], item); --pos);
- for(int i = len; i > pos; --i)
- array[i] = array[i - 1];
- array[pos] = item;
- }
- void swap(Info * a, Info * b)
- {
- Info temp = *a;
- *a = *b;
- *b = temp;
- }
- int compare(Info a, Info b)
- {
- if(a == b)
- return 0;
- if(a > b)
- return 1;
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment