Last active
May 6, 2024 11:47
-
-
Save realyukii/dee100a3b7fa68d9a17e7c0707830e32 to your computer and use it in GitHub Desktop.
Sorting in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sorting alphabet | |
// reference: https://stackoverflow.com/a/64693036/22382954 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(void) | |
{ | |
srand(time(NULL)); | |
char ALLOWED[] = "qwertyuiopasdfghjklzxcvbnm"; | |
char random[26+1]; | |
random[26] = '\0'; | |
int i = 0; | |
int c = 0; | |
int nbAllowed = sizeof(ALLOWED)-1; | |
for(int i = 0;i<26;i++) { | |
c = rand() % nbAllowed; | |
random[i] = ALLOWED[c]; | |
} | |
printf("unsorted ver 1: %s\n", random); | |
// OR just re-order/swap instead of generate the string randomly. | |
int shuffle = 50; | |
for (int j = 0;j < shuffle;j++) { | |
int c = rand() % nbAllowed; | |
for(int i = 0;i < 26-1;i++) { | |
if (i != c) { | |
char temp = ALLOWED[i]; | |
ALLOWED[i] = ALLOWED[c]; | |
ALLOWED[c] = temp; | |
} | |
} | |
} | |
printf("unsorted ver 2: %s\n", ALLOWED); | |
puts("\nsorting..."); | |
int swapped; | |
do { | |
swapped = 0; | |
for(i = 0;i < 26-1;i++) { | |
if (random[i] > random[i+1]) { | |
char temp = random[i]; | |
random[i] = random[i+1]; | |
random[i+1] = temp; | |
swapped = 1; | |
} | |
} | |
} while(swapped); | |
printf("\nsorted ver 1: %s\n", random); | |
do { | |
swapped = 0; | |
for(int i = 0;i < 26-1;i++) { | |
if (ALLOWED[i] > ALLOWED[i+1]) { | |
char temp = ALLOWED[i]; | |
ALLOWED[i] = ALLOWED[i+1]; | |
ALLOWED[i+1] = temp; | |
swapped = 1; | |
} | |
} | |
} while(swapped); | |
printf("sorted ver 2: %s\n", ALLOWED); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define LENGTH 10 | |
int main(void) | |
{ | |
unsigned int array[LENGTH]; | |
puts("unsorted random number"); | |
for (char i = 0;i < LENGTH;i++) { | |
array[i] = (int)random(); | |
printf("%d\n", array[i]); | |
} | |
puts("sorting..."); | |
char swapped; | |
do { | |
swapped = 0; | |
for (char i = 0;i < LENGTH-1;i++) { | |
if (array[i] > array[i+1]) { | |
unsigned int temporary = array[i]; | |
array[i] = array[i+1]; | |
array[i+1] = temporary; | |
swapped = 1; | |
} | |
} | |
} while (swapped); | |
puts("sorted random number - ASCENDING"); | |
for (char i = 0;i < LENGTH;i++) { | |
printf("%d\n", array[i]); | |
} | |
puts("sorting..."); | |
do { | |
swapped = 0; | |
for (char i = 0;i < LENGTH-1;i++) { | |
if (array[i] < array[i+1]) { | |
unsigned int temporary = array[i]; | |
array[i] = array[i+1]; | |
array[i+1] = temporary; | |
swapped = 1; | |
} | |
} | |
} while (swapped); | |
puts("sorted random number - DESCENDING"); | |
for (char i = 0;i < LENGTH;i++) { | |
printf("%d\n", array[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment