KI003.c (4636B)
1 /* Bubble sort (with swap) */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <stdbool.h> 6 7 void inputarr(int **, int); 8 void display(int[], int); 9 int asort(int *, int); 10 int dsort(int *, int); 11 12 int main() 13 { 14 int *arr = NULL, n, choice; 15 do 16 { 17 printf("\nEnter element count: "); 18 if (scanf("%d", &n) != 1 || n < 1) 19 { 20 printf("\nERROR: Invalid element count!!! Try again..."); 21 } 22 else 23 { 24 break; 25 } 26 } while (true); 27 inputarr(&arr, n); 28 29 while (true) 30 { 31 printf("\n1. Sort in Ascending Order." 32 "\n2. Sort in Descending Order" 33 "\n3. Change Input" 34 "\n0. Exit" 35 "\nYour choice: "); 36 do 37 { 38 if (scanf("%d", &choice) != 1) 39 { 40 printf("\nInvalid Choice!!! Try again..."); 41 while (getchar() != '\n') 42 ; 43 } 44 else 45 { 46 break; 47 } 48 } while (true); 49 50 switch (choice) 51 { 52 case 1: 53 printf("\n === Selected Mode: Ascending Order ===\n"); 54 if (!asort(arr, n)) 55 { 56 printf("\nElements are already sorted.\n"); 57 display(arr, n); 58 } 59 printf("\n"); 60 break; 61 case 2: 62 printf("\n === Selected Mode: Descending Order ===\n"); 63 if (!dsort(arr, n)) 64 { 65 printf("\nElements are already sorted.\n"); 66 display(arr, n); 67 } 68 printf("\n"); 69 break; 70 case 3: 71 do 72 { 73 printf("\nEnter element count: "); 74 if (scanf("%d", &n) != 1 || n < 1) 75 { 76 printf("\nERROR: Invalid element count!!! Try again..."); 77 } 78 else 79 { 80 break; 81 } 82 } while (true); 83 free(arr); 84 inputarr(&arr, n); 85 break; 86 case 0: 87 printf("\nExiting Program...\n"); 88 free(arr); 89 return 0; 90 default: 91 printf("\nInvalid Choice!!! Try again..."); 92 } 93 } 94 } 95 96 void inputarr(int **arr, int n) 97 { 98 int i; 99 if (n < 1) 100 { 101 printf("\nInvalid element number."); 102 return; 103 } 104 *arr = (int *)malloc(n * sizeof(int)); 105 if (*arr == NULL) 106 { 107 printf("\nERROR: Memory Allocation falied.\nExiting Program...\n"); 108 exit(1); 109 } 110 for (i = 0; i < n; i++) 111 { 112 printf("Enter Element %d: ", i + 1); 113 scanf("%d", &((*arr)[i])); 114 } 115 } 116 117 void display(int arr[], int n) 118 { 119 int i; 120 printf("["); 121 for (i = 0; i < n; i++) 122 { 123 printf("%d", arr[i]); 124 if (i != n - 1) 125 { 126 printf(", "); 127 } 128 } 129 printf("]"); 130 } 131 132 int asort(int *arr, int n) 133 { 134 bool isSwaped = true; 135 int i, j, tempNum, swapCount = 0; 136 int *temp = (int *)malloc(n * sizeof(int)); 137 if (temp == NULL) 138 { 139 printf("\nERROR: Memory Allocation falied.\nExiting Program...\n"); 140 free(arr); 141 exit(1); 142 } 143 for (i = 0; i < n; i++) 144 { 145 temp[i] = *(arr + i); 146 } 147 for (i = 0; i < n - 1 && isSwaped == true; i++) 148 { 149 isSwaped = false; 150 for (j = 0; j < n - i - 1; j++) 151 { 152 if (temp[j] > temp[j + 1]) 153 { 154 tempNum = temp[j]; 155 temp[j] = temp[j + 1]; 156 temp[j + 1] = tempNum; 157 isSwaped = true; 158 swapCount++; 159 } 160 } 161 } 162 if (swapCount > 0) 163 { 164 display(temp, n); 165 } 166 free(temp); 167 return swapCount; 168 } 169 170 int dsort(int *arr, int n) 171 { 172 bool isSwaped = true; 173 int i, j, tempNum, swapCount = 0; 174 int *temp = (int *)malloc(n * sizeof(int)); 175 if (temp == NULL) 176 { 177 printf("\nERROR: Memory Allocation falied.\nExiting Program...\n"); 178 free(arr); 179 exit(1); 180 } 181 for (i = 0; i < n; i++) 182 { 183 temp[i] = *(arr + i); 184 } 185 for (i = 0; i < n - 1 && isSwaped == true; i++) 186 { 187 isSwaped = false; 188 for (j = 0; j < n - i - 1; j++) 189 { 190 if (temp[j] < temp[j + 1]) 191 { 192 tempNum = temp[j]; 193 temp[j] = temp[j + 1]; 194 temp[j + 1] = tempNum; 195 isSwaped = true; 196 swapCount++; 197 } 198 } 199 } 200 if (swapCount > 0) 201 { 202 display(temp, n); 203 } 204 free(temp); 205 return swapCount; 206 }