Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- array_sort_and_insert_elements.c
- Sorting array of integers in ascending order.
- Insert new integer in ascending sorted array of integers.
- Sorting array of integers in descending order.
- Insert new integer in descending sorted array of integers.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- https://www.facebook.com/dmilicev
- */
- #include <stdio.h>
- // Function to print array, n is number of elements in array arr[]
- // text[] describes the shown array arr[]
- void ShowArray(char text[],int arr[],int n)
- {
- int i;
- printf("%s",text);
- for(i=0;i<n;i++)
- printf("%5d", arr[i]);
- printf("\n");
- }
- // function bubbleSortAscending to sort the array in ascending order
- void bubbleSortAscending(int arr[], int n)
- {
- int i, j, mem;
- for (i = 0; i < n-1; i++)
- for (j = 0; j < n-i-1; j++) // Last i elements are already in place
- if (arr[j] > arr[j+1]) // arr[j] < arr[j+1] for descending order
- {
- mem = arr[j]; // swapping two elements of array
- arr[j] = arr[j+1];
- arr[j+1] = mem;
- }
- }
- // function bubbleSortDescending to sort the array in descending order
- void bubbleSortDescending(int arr[], int n)
- {
- int i, j, mem;
- for (i = 0; i < n-1; i++)
- for (j = 0; j < n-i-1; j++) // Last i elements are already in place
- if (arr[j] < arr[j+1]) // arr[j] > arr[j+1] for ascending order
- {
- mem = arr[j]; // swapping two elements of array
- arr[j] = arr[j+1];
- arr[j+1] = mem;
- }
- }
- // function to insert new element in ascending sorted array
- void insert_new_element_in_ascend_sorted_array(int new, int arr[], int *n )
- {
- int i;
- i = (*n)-1; // i is index and *n is number of elements in array
- while( new<arr[i] && i>=0 ) // from right to left, find index to place new element
- {
- arr[i+1] = arr[i]; // move element arr[i] one place right
- i--; // decrement i
- }
- arr[i+1] = new; // i+1 because we decrement i
- (*n)++; // increment *n because we added one element to array
- }
- // function to insert new element in descending sorted array
- void insert_new_element_in_descend_sorted_array(int new, int arr[], int *n )
- {
- int i;
- i = (*n)-1; // i is index and *n is number of elements in array
- while( new>arr[i] && i>=0 ) // from right to left, find index to place new element
- {
- arr[i+1] = arr[i]; // move element arr[i] one place right
- i--; // decrement i
- }
- arr[i+1] = new; // i+1 because we decrement i
- (*n)++; // increment *n because we added one element to array
- }
- int main(void)
- {
- int i, n, newValue; // i is counter, n is number of elements in array
- int array[100]; // the array of max 100 integers
- printf("\n Please enter size of array: " );
- scanf("%d",&n);
- for( i=0; i<n; i++)
- {
- printf("\n Enter element[%d]: ", i );
- scanf("%d", &array[i] );
- }
- ShowArray("\n The array before sorting in ascend order: \n", array, n );
- // Insert new element in ascend sorted array
- bubbleSortAscending(array,n);
- ShowArray("\n The array after sorting in ascend order: \n", array, n );
- printf("\n Please enter new value: " );
- scanf("%d",&newValue);
- insert_new_element_in_ascend_sorted_array(newValue, array, &n );
- ShowArray("\n New array: \n", array, n );
- // Insert new element in descending sorted array
- bubbleSortDescending(array,n);
- ShowArray("\n The array after sorting in descend order: \n", array, n );
- printf("\n Please enter new value: " );
- scanf("%d",&newValue);
- insert_new_element_in_descend_sorted_array(newValue, array, &n );
- ShowArray("\n New array: \n", array, n );
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment