bsc

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

commit ae717f41387ff6b3229a3f1bc6d069035cd522d3
parent 6c96cceeee466e3c5cfb6ef78d0a5f33a429ff35
Author: Amit Dutta <amitdutta4255@gmail.com>
Date:   Wed, 24 Dec 2025 17:12:37 +0530

[2025-12-24] .\khurapati-idea\KI003.c : Added flagged bubble sort

Diffstat:
Mdocs/index.html | 235+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Akhurapati-idea/KI003.c | 221+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 456 insertions(+), 0 deletions(-)

diff --git a/docs/index.html b/docs/index.html @@ -4038,6 +4038,241 @@ int main() }</div> </div> + <div class="file-item flex items-center justify-between px-3 py-2 hover:bg-white hover:shadow-sm rounded-lg transition-all group border border-transparent hover:border-slate-100 mb-1"> + <div class="flex items-center gap-2.5 min-w-0 cursor-pointer flex-1" onclick="showCode('code-khurapati-idea-KI003-c', 'KI003.c', 'https://github.com/notamitgamer/bsc/blob/main/khurapati-idea/KI003.c')"> + <svg class="w-4 h-4 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"> + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" /> + </svg> + <span class="text-sm text-slate-600 font-medium group-hover:text-blue-600 truncate">KI003.c</span> + </div> + <div class="flex items-center gap-2 opacity-0 group-hover:opacity-100 transition-opacity"> + <a href="https://github.com/notamitgamer/bsc/blob/main/khurapati-idea/KI003.c" target="_blank" class="p-1.5 text-slate-400 hover:text-slate-700 hover:bg-slate-100 rounded-md" title="View on GitHub"> + <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-2.126 1.029-2.935-.103-.253-.446-1.372.097-2.897 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.525.202 2.644.1 2.897.64.809 1.026 1.841 1.026 2.935 0 3.847-2.337 4.687-4.565 4.935.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" clip-rule="evenodd"></path></svg> + </a> + </div> + <!-- Embedded Code Storage --> + <div id="code-khurapati-idea-KI003-c" style="display:none;">/* + * ====================================================================================== + * COPYRIGHT (C) 2025 AMIT DUTTA. ALL RIGHTS RESERVED. + * Repository : https://github.com/notamitgamer/bsc + * License : ESAL-1.0 ( https://aranag.site/license ) + * ====================================================================================== + * [ ACADEMIC INTEGRITY WARNING ] + * The use of this code for academic assignments at ANY educational institution, + * college, or university is STRICTLY PROHIBITED. + * Any other use requires prior written permission from the author. + * Violations will be reported as academic misconduct. + * ====================================================================================== + */ + +/* Bubble sort (with swap) */ + +#include &lt;stdio.h&gt; +#include &lt;stdlib.h&gt; +#include &lt;stdbool.h&gt; + +void inputarr(int **, int); +void display(int[], int); +int asort(int *, int); +int dsort(int *, int); + +int main() +{ + int *arr = NULL, n, choice; + do + { + printf(&quot;\nEnter element count: &quot;); + if (scanf(&quot;%d&quot;, &amp;n) != 1 || n &lt; 1) + { + printf(&quot;\nERROR: Invalid element count!!! Try again...&quot;); + } + else + { + break; + } + } while (true); + inputarr(&amp;arr, n); + + while (true) + { + printf(&quot;\n1. Sort in Ascending Order.&quot; + &quot;\n2. Sort in Descending Order&quot; + &quot;\n3. Change Input&quot; + &quot;\n0. Exit&quot; + &quot;\nYour choice: &quot;); + do + { + if (scanf(&quot;%d&quot;, &amp;choice) != 1) + { + printf(&quot;\nInvalid Choice!!! Try again...&quot;); + while (getchar() != &#x27;\n&#x27;) + ; + } + else + { + break; + } + } while (true); + + switch (choice) + { + case 1: + printf(&quot;\n === Selected Mode: Ascending Order ===\n&quot;); + if (!asort(arr, n)) + { + printf(&quot;\nElements are already sorted.\n&quot;); + display(arr, n); + } + printf(&quot;\n&quot;); + break; + case 2: + printf(&quot;\n === Selected Mode: Descending Order ===\n&quot;); + if (!dsort(arr, n)) + { + printf(&quot;\nElements are already sorted.\n&quot;); + display(arr, n); + } + printf(&quot;\n&quot;); + break; + case 3: + do + { + printf(&quot;\nEnter element count: &quot;); + if (scanf(&quot;%d&quot;, &amp;n) != 1 || n &lt; 1) + { + printf(&quot;\nERROR: Invalid element count!!! Try again...&quot;); + } + else + { + break; + } + } while (true); + free(arr); + inputarr(&amp;arr, n); + break; + case 0: + printf(&quot;\nExiting Program...\n&quot;); + free(arr); + return 0; + default: + printf(&quot;\nInvalid Choice!!! Try again...&quot;); + } + } +} + +void inputarr(int **arr, int n) +{ + int i; + if (n &lt; 1) + { + printf(&quot;\nInvalid element number.&quot;); + return; + } + *arr = (int *)malloc(n * sizeof(int)); + if (*arr == NULL) + { + printf(&quot;\nERROR: Memory Allocation falied.\nExiting Program...\n&quot;); + exit(1); + } + for (i = 0; i &lt; n; i++) + { + printf(&quot;Enter Element %d: &quot;, i + 1); + scanf(&quot;%d&quot;, &amp;((*arr)[i])); + } +} + +void display(int arr[], int n) +{ + int i; + printf(&quot;[&quot;); + for (i = 0; i &lt; n; i++) + { + printf(&quot;%d&quot;, arr[i]); + if (i != n - 1) + { + printf(&quot;, &quot;); + } + } + printf(&quot;]&quot;); +} + +int asort(int *arr, int n) +{ + bool isSwaped = true; + int i, j, tempNum, swapCount = 0; + int *temp = (int *)malloc(n * sizeof(int)); + if (temp == NULL) + { + printf(&quot;\nERROR: Memory Allocation falied.\nExiting Program...\n&quot;); + free(arr); + exit(1); + } + for (i = 0; i &lt; n; i++) + { + temp[i] = *(arr + i); + } + for (i = 0; i &lt; n - 1 &amp;&amp; isSwaped == true; i++) + { + isSwaped = false; + for (j = 0; j &lt; n - i - 1; j++) + { + if (temp[j] &gt; temp[j + 1]) + { + tempNum = temp[j]; + temp[j] = temp[j + 1]; + temp[j + 1] = tempNum; + isSwaped = true; + swapCount++; + } + } + } + if (swapCount &gt; 0) + { + display(temp, n); + } + free(temp); + return swapCount; +} + +int dsort(int *arr, int n) +{ + bool isSwaped = true; + int i, j, tempNum, swapCount = 0; + int *temp = (int *)malloc(n * sizeof(int)); + if (temp == NULL) + { + printf(&quot;\nERROR: Memory Allocation falied.\nExiting Program...\n&quot;); + free(arr); + exit(1); + } + for (i = 0; i &lt; n; i++) + { + temp[i] = *(arr + i); + } + for (i = 0; i &lt; n - 1 &amp;&amp; isSwaped == true; i++) + { + isSwaped = false; + for (j = 0; j &lt; n - i - 1; j++) + { + if (temp[j] &lt; temp[j + 1]) + { + tempNum = temp[j]; + temp[j] = temp[j + 1]; + temp[j + 1] = tempNum; + isSwaped = true; + swapCount++; + } + } + } + if (swapCount &gt; 0) + { + display(temp, n); + } + free(temp); + return swapCount; +}</div> + </div> + </div> </div> diff --git a/khurapati-idea/KI003.c b/khurapati-idea/KI003.c @@ -0,0 +1,220 @@ +/* + * ====================================================================================== + * COPYRIGHT (C) 2025 AMIT DUTTA. ALL RIGHTS RESERVED. + * Repository : https://github.com/notamitgamer/bsc + * License : ESAL-1.0 ( https://aranag.site/license ) + * ====================================================================================== + * [ ACADEMIC INTEGRITY WARNING ] + * The use of this code for academic assignments at ANY educational institution, + * college, or university is STRICTLY PROHIBITED. + * Any other use requires prior written permission from the author. + * Violations will be reported as academic misconduct. + * ====================================================================================== + */ + +/* Bubble sort (with swap) */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +void inputarr(int **, int); +void display(int[], int); +int asort(int *, int); +int dsort(int *, int); + +int main() +{ + int *arr = NULL, n, choice; + do + { + printf("\nEnter element count: "); + if (scanf("%d", &n) != 1 || n < 1) + { + printf("\nERROR: Invalid element count!!! Try again..."); + } + else + { + break; + } + } while (true); + inputarr(&arr, n); + + while (true) + { + printf("\n1. Sort in Ascending Order." + "\n2. Sort in Descending Order" + "\n3. Change Input" + "\n0. Exit" + "\nYour choice: "); + do + { + if (scanf("%d", &choice) != 1) + { + printf("\nInvalid Choice!!! Try again..."); + while (getchar() != '\n') + ; + } + else + { + break; + } + } while (true); + + switch (choice) + { + case 1: + printf("\n === Selected Mode: Ascending Order ===\n"); + if (!asort(arr, n)) + { + printf("\nElements are already sorted.\n"); + display(arr, n); + } + printf("\n"); + break; + case 2: + printf("\n === Selected Mode: Descending Order ===\n"); + if (!dsort(arr, n)) + { + printf("\nElements are already sorted.\n"); + display(arr, n); + } + printf("\n"); + break; + case 3: + do + { + printf("\nEnter element count: "); + if (scanf("%d", &n) != 1 || n < 1) + { + printf("\nERROR: Invalid element count!!! Try again..."); + } + else + { + break; + } + } while (true); + free(arr); + inputarr(&arr, n); + break; + case 0: + printf("\nExiting Program...\n"); + free(arr); + return 0; + default: + printf("\nInvalid Choice!!! Try again..."); + } + } +} + +void inputarr(int **arr, int n) +{ + int i; + if (n < 1) + { + printf("\nInvalid element number."); + return; + } + *arr = (int *)malloc(n * sizeof(int)); + if (*arr == NULL) + { + printf("\nERROR: Memory Allocation falied.\nExiting Program...\n"); + exit(1); + } + for (i = 0; i < n; i++) + { + printf("Enter Element %d: ", i + 1); + scanf("%d", &((*arr)[i])); + } +} + +void display(int arr[], int n) +{ + int i; + printf("["); + for (i = 0; i < n; i++) + { + printf("%d", arr[i]); + if (i != n - 1) + { + printf(", "); + } + } + printf("]"); +} + +int asort(int *arr, int n) +{ + bool isSwaped = true; + int i, j, tempNum, swapCount = 0; + int *temp = (int *)malloc(n * sizeof(int)); + if (temp == NULL) + { + printf("\nERROR: Memory Allocation falied.\nExiting Program...\n"); + free(arr); + exit(1); + } + for (i = 0; i < n; i++) + { + temp[i] = *(arr + i); + } + for (i = 0; i < n - 1 && isSwaped == true; i++) + { + isSwaped = false; + for (j = 0; j < n - i - 1; j++) + { + if (temp[j] > temp[j + 1]) + { + tempNum = temp[j]; + temp[j] = temp[j + 1]; + temp[j + 1] = tempNum; + isSwaped = true; + swapCount++; + } + } + } + if (swapCount > 0) + { + display(temp, n); + } + free(temp); + return swapCount; +} + +int dsort(int *arr, int n) +{ + bool isSwaped = true; + int i, j, tempNum, swapCount = 0; + int *temp = (int *)malloc(n * sizeof(int)); + if (temp == NULL) + { + printf("\nERROR: Memory Allocation falied.\nExiting Program...\n"); + free(arr); + exit(1); + } + for (i = 0; i < n; i++) + { + temp[i] = *(arr + i); + } + for (i = 0; i < n - 1 && isSwaped == true; i++) + { + isSwaped = false; + for (j = 0; j < n - i - 1; j++) + { + if (temp[j] < temp[j + 1]) + { + tempNum = temp[j]; + temp[j] = temp[j + 1]; + temp[j + 1] = tempNum; + isSwaped = true; + swapCount++; + } + } + } + if (swapCount > 0) + { + display(temp, n); + } + free(temp); + return swapCount; +}+ \ No newline at end of file
© 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