Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* insertion sort */
- void insertion_sort(int arr[], int n)
- {
- for(int i = 0; i < n; i++)
- {
- int bufor = arr[i]; //bufor stores the element whose left side is checked
- int j = i;
- //loop checks whether the element on left side is greater or smaller than the current element
- while(j > 0 && bufor < arr[j-1])
- {
- arr[j] = arr[j-1]; // moving the left side element to one position forward
- j = j - 1;
- }
- arr[j] = bufor; //moving current element to its correct position
- }
- }
- int main(int argc, char *argv[])
- {
- int A [100];
- int i, n ,j ,key;
- printf("Enter the number of elements you want to sort = ");
- scanf("%d", &n);
- for (i=0; i<n; i++)
- {
- printf("\nEnter the element %d: ", i+1);
- scanf("%d", &A[i]); // not put space after d :>
- fflush(stdin);
- }
- insertion_sort(A, n);
- for (i=0; i<n; i++)
- {
- printf("%d\n", A[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement