Advertisement
dmilicev

Three smallest elements in the array of integers.c

Sep 22nd, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. // Find three smallest elements in the array of integers without sorting array
  2. // https://www.geeksforgeeks.org/find-the-largest-three-elements-in-an-array/
  3. // This code is edited by Ayush Singla(@ayusin51) and Dragan Milicev (dmilicev@gmail.com)
  4.  
  5. #include <stdio.h>
  6. #include <limits.h> // For INT_MAX
  7.  
  8. // Function to print array
  9. void ShowArray(char text[],int arr[],int n)
  10. {
  11.     int i;
  12.  
  13.     printf("\n%s",text);
  14.     for(i=0;i<n;i++)
  15.         printf("%5d", arr[i]);
  16.  
  17.     printf("\n");
  18. }
  19.  
  20. // Function to print three smallest elements
  21. void print3smallest(int arr[], int arr_size)
  22. {
  23.     int i, first, second, third;
  24.  
  25.     if (arr_size < 3)
  26.     {
  27.         printf("\n Invalid input. There should be atleast three elements in array. \n");
  28.         return;
  29.     }
  30.  
  31. // 1) Initialize the smallest three elements as plus infinite.
  32. //    first = second = third = +Infinity
  33.  
  34.     first = second = third = INT_MAX;
  35.  
  36.     printf("\n How algorithm work: \n");
  37.     printf("\n  i  arr[i]       first     second        third \n"); // header
  38.  
  39. // 2) Iterate through all elements of array.
  40.  
  41.     for (i = 0; i < arr_size ; i ++)
  42.     {
  43.         if (arr[i] < first)         // Let current array element be arr[i]
  44.         {                           // If current element is smaller than first
  45.             third = second;         // then update third, second and first
  46.             second = first;         // This order of assignment is important
  47.             first = arr[i];
  48.         }
  49.         else if (arr[i] < second)   // If arr[i] is in between first and second
  50.         {                           // then update second
  51.             third = second;
  52.             second = arr[i];
  53.         }
  54.         else if (arr[i] < third)    // If arr[i] smaller then third
  55.             third = arr[i];         // then update third
  56.  
  57.         // print how algorithm work
  58.         printf("\n %2d  %4d %12d  %12d  %12d \n",i,arr[i],first,second,third);
  59.     }
  60.  
  61. // 3) Print first, second and third.
  62.  
  63.     printf("\n Three smallest elements are: %3d %3d %3d \n", first, second, third);
  64. }
  65.  
  66.  
  67. // Driver program to test above function
  68. int main(void)
  69. {
  70.     int arr[] = {12, 13, 1, 10, 34, 2};     // try different one
  71. //  int arr[]={9,2,7,7,8,6,5,1,3};
  72. //  int arr[]={7,2,7,7,8,6,5,1,3};
  73. //  int arr[]={4,2,9,7,8,6,5,1,9};
  74. //  int arr[] = {1,1,1,1,1,1};
  75. //  int arr[] = {1,1};
  76.     int n = sizeof(arr)/sizeof(arr[0]);     // a way to get n, number of elements in array
  77.     int i;
  78.  
  79.     printf(" n = %d \n", n);            // print number of elements in array
  80.  
  81.     ShowArray(" Array is: ",arr,n);
  82.  
  83.     print3smallest(arr, n);                 // print three smallest elements in the array
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement