Advertisement
saurav_kalsoor

LP Assignment 3 - BT19CSE045

Apr 2nd, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 64.19 KB | None | 0 0
  1. // Author : Saurav Kalsoor
  2. // Created On : 28 / 03/ 2022
  3.  
  4. #include <stdio.h>
  5. #include<unistd.h>
  6.  
  7.  
  8. void swap(int *xp, int *yp)
  9. {
  10.     int temp = *xp;
  11.     *xp = *yp;
  12.     *yp = temp;
  13. }
  14.  
  15. void selectionSort(int arr[], int n)
  16. {
  17.     int i, j, min_idx;
  18.  
  19.     // One by one move boundary of unsorted subarray
  20.     for (i = 0; i < n-1; i++)
  21.     {
  22.         // Find the minimum element in unsorted array
  23.         min_idx = i;
  24.         for (j = i+1; j < n; j++)
  25.           if (arr[j] < arr[min_idx])
  26.             min_idx = j;
  27.  
  28.         // Swap the found minimum element with the first element
  29.         swap(&arr[min_idx], &arr[i]);
  30.     }
  31. }
  32.   // A function to implement bubble sort
  33. void bubbleSort(int arr[], int n)
  34. {
  35.    int i, j;
  36.    for (i = 0; i < n-1; i++)    
  37.  
  38.        // Last i elements are already in place  
  39.        for (j = 0; j < n-i-1; j++)
  40.            if (arr[j] > arr[j+1])
  41.               swap(&arr[j], &arr[j+1]);
  42. }
  43.  
  44. // A function to implement bubble sort
  45. void recursiveBubbleSort(int arr[], int n)
  46. {
  47.     // Base case
  48.     if (n == 1)
  49.         return;
  50.  
  51.     // One pass of bubble sort. After
  52.     // this pass, the largest element
  53.     // is moved (or bubbled) to end.
  54.     for (int i=0; i<n-1; i++)
  55.         if (arr[i] > arr[i+1])
  56.             swap(&arr[i], &arr[i+1]),sleep(0);
  57.  
  58.     // Largest element is fixed,
  59.     // recur for remaining array
  60.     recursiveBubbleSort(arr, n-1);
  61. }
  62. /* Function to sort an array using insertion sort*/
  63. void insertionSort(int arr[], int n)
  64. {
  65.     int i, key, j;
  66.     for (i = 1; i < n; i++) {
  67.         key = arr[i];
  68.         j = i - 1;
  69.  
  70.         /* Move elements of arr[0..i-1], that are
  71.           greater than key, to one position ahead
  72.           of their current position */
  73.         while (j >= 0 && arr[j] > key) {
  74.             arr[j + 1] = arr[j];
  75.             j = j - 1;
  76.             sleep(0);
  77.         }
  78.         arr[j + 1] = key;
  79.     }
  80. }
  81.  
  82.  
  83. // Recursive function to sort an array using
  84. // insertion sort
  85. void insertionSortRecursive(int arr[], int n)
  86. {
  87.     // Base case
  88.     if (n <= 1)
  89.         return;
  90.  
  91.     // Sort first n-1 elements
  92.     insertionSortRecursive( arr, n-1 );
  93.  
  94.     // Insert last element at its correct position
  95.     // in sorted array.
  96.     int last = arr[n-1];
  97.     int j = n-2;
  98.  
  99.     /* Move elements of arr[0..i-1], that are
  100.     greater than key, to one position ahead
  101.     of their current position */
  102.     while (j >= 0 && arr[j] > last)
  103.     {
  104.         arr[j+1] = arr[j];
  105.         j--;
  106.         sleep(0);
  107.     }
  108.     arr[j+1] = last;
  109. }
  110.  
  111.  
  112. /* Function to print an array */
  113. void printArray(int arr[], int size)
  114. {
  115.     int i;
  116.     for (i=0; i < size; i++)
  117.         printf("%d ", arr[i]);
  118.     printf("\n");
  119. }
  120.  
  121.  
  122. void merge(int arr[], int l, int m, int r);
  123.  
  124. /* l is for left index and r is
  125.  right index of the sub-array
  126.   of arr to be sorted */
  127. void mergeSort(int arr[], int l, int r)
  128. {
  129.    if (l < r)
  130.    {
  131.       // Same as (l+r)/2 but avoids
  132.       // overflow for large l & h
  133.       int m = l+(r-l)/2;
  134.       mergeSort(arr, l, m);
  135.       mergeSort(arr, m+1, r);
  136.       merge(arr, l, m, r);
  137.    }
  138. }
  139.  
  140. /* Function to merge the two haves arr[l..m]
  141.  and arr[m+1..r] of array arr[] */
  142. void merge(int arr[], int l, int m, int r)
  143. {
  144.     int i, j, k;
  145.     int n1 = m - l + 1;
  146.     int n2 =  r - m;
  147.  
  148.     /* create temp arrays */
  149.     int L[n1], R[n2];
  150.  
  151.     /* Copy data to temp arrays L[] and R[] */
  152.     for (i = 0; i < n1; i++)
  153.         L[i] = arr[l + i];
  154.     for (j = 0; j < n2; j++)
  155.         R[j] = arr[m + 1+ j];
  156.  
  157.     /* Merge the temp arrays back into arr[l..r]*/
  158.     i = 0;
  159.     j = 0;
  160.     k = l;
  161.     while (i < n1 && j < n2)
  162.     {
  163.         if (L[i] <= R[j])
  164.         {
  165.             arr[k] = L[i];
  166.             i++;
  167.         }
  168.         else
  169.         {
  170.             arr[k] = R[j];
  171.             j++;
  172.         }
  173.         k++;
  174.     }
  175.  
  176.     /* Copy the remaining elements
  177.     of L[], if there are any */
  178.     while (i < n1)
  179.     {
  180.         arr[k] = L[i];
  181.         i++;
  182.         k++;
  183.     }
  184.  
  185.     /* Copy the remaining elements
  186.     of R[], if there are any */
  187.     while (j < n2)
  188.     {
  189.         arr[k] = R[j];
  190.         j++;
  191.         k++;
  192.     }
  193. }
  194.  
  195. int partition (int arr[], int low, int high)
  196. {
  197.     int pivot = arr[high]; // pivot
  198.     int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
  199.  
  200.     for (int j = low; j <= high - 1; j++)
  201.     {
  202.         // If current element is smaller than the pivot
  203.         if (arr[j] < pivot)
  204.         {
  205.             i++; // increment index of smaller element
  206.             swap(&arr[i], &arr[j]);
  207.         }
  208.     }
  209.     swap(&arr[i + 1], &arr[high]);
  210.     return (i + 1);
  211. }
  212.  
  213. /* The main function that implements QuickSort
  214. arr[] --> Array to be sorted,
  215. low --> Starting index,
  216. high --> Ending index */
  217. void quickSort(int arr[], int low, int high)
  218. {
  219.     if (low < high)
  220.     {
  221.         /* pi is partitioning index, arr[p] is now
  222.         at right place */
  223.         int pi = partition(arr, low, high);
  224.  
  225.         // Separately sort elements before
  226.         // partition and after partition
  227.         quickSort(arr, low, pi - 1);
  228.         quickSort(arr, pi + 1, high);
  229.     }
  230. }
  231.  
  232. // To heapify a subtree rooted with node i which is
  233. // an index in arr[]. n is size of heap
  234. void heapify(int arr[], int n, int i)
  235. {
  236.     int largest = i; // Initialize largest as root
  237.     int l = 2 * i + 1; // left = 2*i + 1
  238.     int r = 2 * i + 2; // right = 2*i + 2
  239.  
  240.     // If left child is larger than root
  241.     if (l < n && arr[l] > arr[largest])
  242.         largest = l;
  243.  
  244.     // If right child is larger than largest so far
  245.     if (r < n && arr[r] > arr[largest])
  246.         largest = r;
  247.  
  248.     // If largest is not root
  249.     if (largest != i) {
  250.         swap(&arr[i], &arr[largest]);
  251.  
  252.         // Recursively heapify the affected sub-tree
  253.         heapify(arr, n, largest);
  254.     }
  255. }
  256.  
  257. // main function to do heap sort
  258. void heapSort(int arr[], int n)
  259. {
  260.     // Build heap (rearrange array)
  261.     for (int i = n / 2 - 1; i >= 0; i--)
  262.         heapify(arr, n, i);
  263.  
  264.     // One by one extract an element from heap
  265.     for (int i = n - 1; i > 0; i--) {
  266.         // Move current root to end
  267.         swap(&arr[0], &arr[i]);
  268.  
  269.         // call max heapify on the reduced heap"Sorted array: \n");
  270.     printArray(arr, n);
  271.     return 0;
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement