dmilicev

array_sort_and_insert_elements.c

Jan 9th, 2020
221
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None
  1. /*
  2.  
  3.     array_sort_and_insert_elements.c
  4.  
  5.  
  6.     Sorting array of integers in ascending order.
  7.  
  8.     Insert new integer in ascending sorted array of integers.
  9.  
  10.  
  11.     Sorting array of integers in descending order.
  12.  
  13.     Insert new integer in descending sorted array of integers.
  14.  
  15.  
  16.     You can find all my C programs at Dragan Milicev's pastebin:
  17.  
  18.     https://pastebin.com/u/dmilicev
  19.  
  20.     https://www.facebook.com/dmilicev
  21.  
  22. */
  23.  
  24. #include <stdio.h>
  25.  
  26. // Function to print array, n is number of elements in array arr[]
  27. // text[] describes the shown array arr[]
  28. void ShowArray(char text[],int arr[],int n)
  29. {
  30.     int i;
  31.  
  32.     printf("%s",text);
  33.     for(i=0;i<n;i++)
  34.         printf("%5d", arr[i]);
  35.  
  36.     printf("\n");
  37. }
  38.  
  39. // function bubbleSortAscending to sort the array in ascending order
  40. void bubbleSortAscending(int arr[], int n)
  41. {
  42.     int i, j, mem;
  43.  
  44.     for (i = 0; i < n-1; i++)
  45.         for (j = 0; j < n-i-1; j++)     // Last i elements are already in place
  46.             if (arr[j] > arr[j+1])      // arr[j] < arr[j+1] for descending order
  47.             {
  48.                 mem      = arr[j];      // swapping two elements of array
  49.                 arr[j]   = arr[j+1];
  50.                 arr[j+1] = mem;
  51.             }
  52. }
  53.  
  54. // function bubbleSortDescending to sort the array in descending order
  55. void bubbleSortDescending(int arr[], int n)
  56. {
  57.     int i, j, mem;
  58.  
  59.     for (i = 0; i < n-1; i++)
  60.         for (j = 0; j < n-i-1; j++)     // Last i elements are already in place
  61.             if (arr[j] < arr[j+1])      // arr[j] > arr[j+1] for ascending order
  62.             {
  63.                 mem      = arr[j];      // swapping two elements of array
  64.                 arr[j]   = arr[j+1];
  65.                 arr[j+1] = mem;
  66.             }
  67. }
  68.  
  69. // function to insert new element in ascending sorted array
  70. void insert_new_element_in_ascend_sorted_array(int new, int arr[], int *n )
  71. {
  72.     int i;
  73.  
  74.     i = (*n)-1;                 // i is index and *n is number of elements in array
  75.  
  76.     while( new<arr[i] && i>=0 ) // from right to left, find index to place new element
  77.     {
  78.        arr[i+1] = arr[i];       // move element arr[i] one place right
  79.        i--;                     // decrement i
  80.     }
  81.  
  82.     arr[i+1] = new;             // i+1 because we decrement i
  83.  
  84.     (*n)++;                     // increment *n because we added one element to array
  85. }
  86.  
  87. // function to insert new element in descending sorted array
  88. void insert_new_element_in_descend_sorted_array(int new, int arr[], int *n )
  89. {
  90.     int i;
  91.  
  92.     i = (*n)-1;                 // i is index and *n is number of elements in array
  93.  
  94.     while( new>arr[i] && i>=0 ) // from right to left, find index to place new element
  95.     {
  96.        arr[i+1] = arr[i];       // move element arr[i] one place right
  97.        i--;                     // decrement i
  98.     }
  99.  
  100.     arr[i+1] = new;             // i+1 because we decrement i
  101.  
  102.     (*n)++;                     // increment *n because we added one element to array
  103. }
  104.  
  105.  
  106. int main(void)
  107. {
  108.     int i, n, newValue;         // i is counter, n is number of elements in array
  109.     int array[100];             // the array of max 100 integers
  110.  
  111.     printf("\n Please enter size of array: " );
  112.     scanf("%d",&n);
  113.  
  114.     for( i=0; i<n; i++)
  115.     {
  116.         printf("\n Enter element[%d]: ", i );
  117.         scanf("%d", &array[i] );
  118.     }
  119.  
  120.     ShowArray("\n The array before sorting in ascend order: \n", array, n );
  121.  
  122.  
  123.     // Insert new element in ascend sorted array
  124.  
  125.     bubbleSortAscending(array,n);
  126.  
  127.     ShowArray("\n The array after sorting in ascend order: \n", array, n );
  128.  
  129.     printf("\n Please enter new value: " );
  130.     scanf("%d",&newValue);
  131.  
  132.     insert_new_element_in_ascend_sorted_array(newValue, array, &n );
  133.  
  134.     ShowArray("\n New array: \n", array, n );
  135.  
  136.  
  137.     // Insert new element in descending sorted array
  138.  
  139.     bubbleSortDescending(array,n);
  140.  
  141.     ShowArray("\n The array after sorting in descend order: \n", array, n );
  142.  
  143.     printf("\n Please enter new value: " );
  144.     scanf("%d",&newValue);
  145.  
  146.     insert_new_element_in_descend_sorted_array(newValue, array, &n );
  147.  
  148.     ShowArray("\n New array: \n", array, n );
  149.  
  150.  
  151.     return 0;
  152.  
  153. } // main()
RAW Paste Data Copied