Advertisement
dmilicev

sorting_array_of_integers_v1.c

Oct 4th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.89 KB | None | 0 0
  1. /*
  2.  
  3. sorting_array_of_integers_v1.c
  4.  
  5. Sorting array of integers in different ways,
  6. in ascending or descending order
  7.  
  8. - Bubble sort
  9.  
  10. - Selection sort
  11.  
  12. - Insertion sort
  13.  
  14. - quick sort
  15.  
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <limits.h> // For INT_MAX and INT_MIN
  20. #include <time.h>   // for random numbers, for function rand()
  21.  
  22. // Function to print array, n is number of elements in array arr[]
  23. // text[] describes the shown array arr[]
  24. void ShowArray(char text[],int arr[],int n)
  25. {
  26.     int i;
  27.  
  28.     printf("%s",text);
  29.     for(i=0;i<n;i++)
  30.         printf("%5d", arr[i]);
  31.  
  32.     printf("\n");
  33. }
  34.  
  35. // Function to print three smallest elements
  36. void print3smallest(int arr[], int arr_size)
  37. {
  38.     int i, first, second, third;
  39.  
  40.     if (arr_size < 3)
  41.     {
  42.         printf("\n Invalid input. There should be atleast three elements in array. \n");
  43.         return;
  44.     }
  45.  
  46. // 1) Initialize the smallest three elements as plus infinite.
  47. //    first = second = third = +Infinity
  48.  
  49.     first = second = third = INT_MAX;
  50.  
  51.     printf("\n How algorithm work: \n");
  52.     printf("\n  i  arr[i]       first     second        third \n"); // header
  53.  
  54. // 2) Iterate through all elements of array.
  55.  
  56.     for (i = 0; i < arr_size ; i ++)
  57.     {
  58.         if (arr[i] < first)         // Let current array element be arr[i]
  59.         {                           // If current element is smaller than first
  60.             third = second;         // then update third, second and first
  61.             second = first;         // This order of assignment is important
  62.             first = arr[i];
  63.         }
  64.         else if (arr[i] < second)   // If arr[i] is in between first and second
  65.         {                           // then update second
  66.             third = second;
  67.             second = arr[i];
  68.         }
  69.         else if (arr[i] < third)    // If arr[i] smaller then third
  70.             third = arr[i];         // then update third
  71.  
  72.         // print how algorithm work
  73.         printf("\n %2d  %4d %12d  %12d  %12d \n",i,arr[i],first,second,third);
  74.     }
  75.  
  76. // 3) Print first, second and third.
  77.  
  78.     printf("\n Three smallest elements are: %3d %3d %3d \n", first, second, third);
  79. }
  80.  
  81.  
  82. // function returns the smallest element of the array of arr_size integers
  83. int minimum(int arr[], int arr_size)
  84. {
  85.     // Let's suppose that the smallest element of array is the largest possible integer
  86.     int i, smallest=INT_MAX;
  87.  
  88.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  89.         if (arr[i] < smallest)      // if current element arr[i] is smaller than smallest
  90.             smallest=arr[i];        // then arr[i] becomes the smallest
  91.  
  92.     return smallest;
  93. }
  94.  
  95.  
  96. // function returns the largest element of the array of arr_size integers
  97. int maximum(int arr[], int arr_size)
  98. {
  99.     // Let's suppose that the largest element of array is the smallest possible integer
  100.     int i, largest=INT_MIN;
  101.  
  102.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  103.         if (arr[i] > largest)       // if current element arr[i] is larger than largest
  104.             largest=arr[i];         // then arr[i] becomes the largest
  105.  
  106.     return largest;
  107. }
  108.  
  109.  
  110. // function returns the sum of all elements of the array of arr_size integers
  111. int sum(int arr[], int arr_size)
  112. {
  113.     int i, sum=0;   // The starting value of the sum is zero
  114.  
  115.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  116.         sum=sum+arr[i];             // new sum is old sum plus current element arr[i]
  117.  
  118.     return sum;
  119. }
  120.  
  121.  
  122. // function returns the average of all elements of the array of arr_size integers
  123. float average(int arr[], int arr_size)
  124. {
  125.     int i, sum=0;   // The starting value of the sum is zero
  126.     float average;
  127.  
  128.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  129.         sum=sum+arr[i];             // new sum is old sum plus current element arr[i]
  130.  
  131.     average=sum/arr_size;
  132.  
  133.     return average;
  134. }
  135.  
  136.  
  137. // function to calculate the median of the array
  138. float median(int array[],int n)
  139. {
  140.     float median=0;
  141.  
  142.     // if number of elements are even
  143.     if(n%2 == 0)
  144.         median = (array[(n-1)/2] + array[n/2])/2.0;
  145.     // if number of elements are odd
  146.     else
  147.         median = array[n/2];
  148.  
  149.     return median;
  150. }
  151.  
  152.  
  153. // Function to swap two integers, with pointers
  154. // https://www.geeksforgeeks.org/c-program-swap-two-numbers/
  155. void swap(int *xp, int *yp)
  156. {
  157.     int temp = *xp;
  158.     *xp = *yp;
  159.     *yp = temp;
  160. }
  161.  
  162.  
  163. // function bubbleSort to sort the array in ascending order
  164. // https://www.geeksforgeeks.org/bubble-sort/
  165. void bubbleSort(int arr[], int n)
  166. {
  167.     int i, j;
  168.  
  169.     for (i = 0; i < n-1; i++)
  170.         for (j = 0; j < n-i-1; j++)     // Last i elements are already in place
  171.             if (arr[j] > arr[j+1])      // arr[j] < arr[j+1] for descending order
  172.                 swap(&arr[j], &arr[j+1]);
  173. }
  174.  
  175.  
  176. // function selectionSort to sort the array in ascending order
  177. // https://www.geeksforgeeks.org/selection-sort/
  178. void selectionSort(int arr[], int n)
  179. {
  180.     int i, j, min_index;
  181.  
  182.     // One by one move boundary of unsorted subarray
  183.     for (i = 0; i < n-1; i++)
  184.     {
  185.         // Find the minimum element in unsorted array
  186.         min_index = i;
  187.         for (j = i+1; j < n; j++)
  188.             if (arr[j] < arr[min_index])    // arr[j] > arr[min_index] for descending order
  189.                 min_index = j;
  190.  
  191.         // Swap the found minimum element with the first element
  192.         swap(&arr[min_index], &arr[i]);
  193.     }
  194. }
  195.  
  196.  
  197. // function insertionSort to sort the array in ascending order
  198. // https://www.geeksforgeeks.org/insertion-sort/
  199. void insertionSort(int arr[], int n)
  200. {
  201.     int i, key, j;
  202.  
  203.     for (i = 1; i < n; i++)
  204.     {
  205.         key = arr[i];
  206.         j = i - 1;
  207.  
  208.         /* Move elements of arr[0..i-1], that are
  209.           greater than key, to one position ahead
  210.           of their current position */
  211.         while (j >= 0 && arr[j] > key)  // arr[j] < key for descending order
  212.         {
  213.             arr[j + 1] = arr[j];
  214.             j = j - 1;
  215.         }
  216.  
  217.         arr[j + 1] = key;
  218.     }
  219. }
  220.  
  221.  
  222. // comparator function for qsort
  223. // https://www.geeksforgeeks.org/comparator-function-of-qsort-in-c/
  224. int comparator_function_for_qsort(const void *a, const void *b)
  225. {
  226.     return ( *(int*)a - *(int*)b );
  227.  
  228. //  return ( *(int*)b - *(int*)a ); // for descending order
  229. }
  230.  
  231.  
  232. void make_array_of_n_integers_beetween_min_and_max(int array[], int n, int min, int max)
  233. {
  234.     int i;
  235.  
  236. //int rand(void) creates a pseudo-random number in the range of 0 to RAND_MAX
  237. //RAND_MAX is defined in stdlib.h and is the largest number rand will return (same as INT_MAX). for (i=0; i<n; i++)
  238.  
  239. //  for positive and negative numbers beetween min and max
  240.  
  241.     for(i=0;i<n;i++)
  242.         array[i] = (rand() % (max + 1 - min)) + min;
  243.  
  244. //      array[i] = rand() % max;    // for only positive numbers beetween 0 and max
  245. }
  246.  
  247.  
  248. // Main program to test above functions
  249. int main(void)
  250. {
  251.     int arr[10];                        // array of 10 integers
  252.     int n = sizeof(arr)/sizeof(arr[0]); // a way to get n, number of elements in array arr[]
  253.     int i, min=-100, max=100;           // min and max are minimum and maximum value of random integers
  254.  
  255.     srand(time(NULL));  // Initialization for random numbers, should only be called once.
  256.  
  257.     make_array_of_n_integers_beetween_min_and_max(arr,n,min,max);
  258.  
  259.     // print number of elements in array, min and max
  260.     printf("\n Array arr[] has n = %d integers between min = %d and max = %d \n", n, min, max);
  261.  
  262.     ShowArray("\n Unsorted array of random integers between min and max is: \n\n",arr,n);
  263.  
  264.     printf("\n\n The smallest integer in array is %d \n",minimum(arr,n));
  265.  
  266.     printf("\n The largest integer in array is %d \n",maximum(arr,n));
  267.  
  268.     printf("\n The sum of all integers in array is %d \n",sum(arr,n));
  269.  
  270.     printf("\n The average of all integers in array is %f \n",average(arr,n));
  271.  
  272.     printf("\n ------------------------------------------------------------ \n");
  273.  
  274.     bubbleSort(arr,n);
  275.  
  276.     ShowArray("\n Array sorted by bubble sort is: \n\n",arr,n);
  277.  
  278.     printf("\n The median of all integers in array is %f \n",median(arr,n));
  279.  
  280.     printf("\n ------------------------------------------------------------ \n");
  281.  
  282. // since the array is already sorted we need to recreate the unsorted array
  283.     make_array_of_n_integers_beetween_min_and_max(arr,n,min,max);
  284.  
  285.     ShowArray("\n Unsorted array of random integers between min and max is: \n\n",arr,n);
  286.  
  287.     selectionSort(arr,n);
  288.  
  289.     ShowArray("\n Array sorted by selection sort is: \n\n",arr,n);
  290.  
  291.     printf("\n ------------------------------------------------------------ \n");
  292.  
  293. // since the array is already sorted we need to recreate the unsorted array
  294.     make_array_of_n_integers_beetween_min_and_max(arr,n,min,max);
  295.  
  296.     ShowArray("\n Unsorted array of random integers between min and max is: \n\n",arr,n);
  297.  
  298.     insertionSort(arr,n);
  299.  
  300.     ShowArray("\n Array sorted by insertion sort is: \n\n",arr,n);
  301.  
  302.     printf("\n ------------------------------------------------------------ \n");
  303.  
  304. // since the array is already sorted we need to recreate the unsorted array
  305.     make_array_of_n_integers_beetween_min_and_max(arr,n,min,max);
  306.  
  307.     ShowArray("\n Unsorted array of random integers between min and max is: \n\n",arr,n);
  308.  
  309.     qsort(arr, n, sizeof(int), comparator_function_for_qsort);
  310.  
  311.     ShowArray("\n Array sorted by qsort is: \n\n",arr,n);
  312.  
  313.     printf("\n ------------------------------------------------------------ \n");
  314.  
  315.  
  316.     return 0;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement