Advertisement
Sierra_ONE

SortingAlgorithms

Feb 15th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | Source Code | 0 0
  1. #include <stdio.h>
  2.  
  3. // Bubble Sort
  4. void bubbleSort(int arr[], int n) {
  5.     for (int i = 0; i < n - 1; i++) {
  6.         // Flag to optimize the algorithm by checking if any swaps are made in the inner loop
  7.         int swapped = 0;
  8.  
  9.         for (int j = 0; j < n - i - 1; j++) {
  10.             // Push heavier elements to the end of the array
  11.             if (arr[j] > arr[j + 1]) {
  12.                 // Swap elements
  13.                 int temp = arr[j];
  14.                 arr[j] = arr[j + 1];
  15.                 arr[j + 1] = temp;
  16.                 swapped = 1;
  17.             }
  18.         }
  19.  
  20.         // If no two elements were swapped in the inner loop, the array is already sorted
  21.         if (swapped == 0)
  22.             break;
  23.     }
  24. }
  25.  
  26. // Insertion Sort Shift Variation
  27. void insertionSort(int arr[], int n) {
  28.     for (int i = 1; i < n; i++) {
  29.         int key = arr[i];
  30.         int j = i - 1;
  31.  
  32.         // Shift elements in the inner loop and insert the current element in its proper position in the outer loop
  33.         while (j >= 0 && arr[j] > key) {
  34.             arr[j + 1] = arr[j];
  35.             j--;
  36.         }
  37.         arr[j + 1] = key;
  38.     }
  39. }
  40.  
  41. // Selection Sort
  42. void selectionSort(int arr[], int n) {
  43.     for (int i = 0; i < n - 1; i++) {
  44.         int min_idx = i;
  45.  
  46.         for (int j = i + 1; j < n; j++) {
  47.             // Find the index of the minimum element in the unsorted part of the array
  48.             if (arr[j] < arr[min_idx])
  49.                 min_idx = j;
  50.         }
  51.  
  52.         // Swap the found minimum element with the first element
  53.         int temp = arr[i];
  54.         arr[i] = arr[min_idx];
  55.         arr[min_idx] = temp;
  56.     }
  57. }
  58.  
  59. // Function to print an array
  60. void printArray(int arr[], int size) {
  61.     for (int i = 0; i < size; i++)
  62.         printf("%d ", arr[i]);
  63.     printf("\n");
  64. }
  65.  
  66. int main() {
  67.     int arr[] = {64, 34, 25, 12, 22, 11, 90};
  68.     int n = sizeof(arr) / sizeof(arr[0]);
  69.  
  70.     printf("Original array: ");
  71.     printArray(arr, n);
  72.  
  73.     // Sorting using Bubble Sort
  74.     bubbleSort(arr, n);
  75.     printf("Array sorted using Bubble Sort: ");
  76.     printArray(arr, n);
  77.  
  78.     // Resetting the array
  79.     int resetArr[] = {64, 34, 25, 12, 22, 11, 90};
  80.  
  81.     // Sorting using Insertion Sort
  82.     insertionSort(resetArr, n);
  83.     printf("Array sorted using Insertion Sort: ");
  84.     printArray(resetArr, n);
  85.  
  86.     // Resetting the array
  87.     int resetArr2[] = {64, 34, 25, 12, 22, 11, 90};
  88.  
  89.     // Sorting using Selection Sort
  90.     selectionSort(resetArr2, n);
  91.     printf("Array sorted using Selection Sort: ");
  92.     printArray(resetArr2, n);
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement