bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

KI003.c (4827B)


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