Last active
December 2, 2022 17:20
-
-
Save muhammedeminoglu/c69cabee84fd2d1fb3dcceb0d09344b7 to your computer and use it in GitHub Desktop.
this code generates 10000 random numbers and sorts them with Bubble Sort Algorithm. I used an Array Here. This code could be optimized for if array has already sorted. But my point is just show simple algorithm
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> | |
#define SIZE 10000 | |
int myArray[SIZE - 1]; | |
void bubbleSort(int x[]) | |
{ | |
int i, j; | |
for(i = 1; i < SIZE; i++) | |
{ | |
for( j = 0; j < SIZE - 1; j++) | |
{ | |
if(myArray[j] > myArray[j+1]) | |
swapf(j, j+1); | |
} | |
} | |
} | |
void printSorted() | |
{ | |
int i; | |
for( i = 0; i < SIZE - 1; i++) | |
{ | |
printf("%d\n", myArray[i]); | |
} | |
} | |
void swapf(int x, int y) | |
{ | |
int temp; | |
temp = myArray[x]; | |
myArray[x] = myArray[y]; | |
myArray[y] = temp; | |
} | |
void init() | |
{ | |
int i; | |
for( i = 0; i < SIZE - 1; i++) | |
{ | |
myArray[i] = rand()%10000; | |
} | |
} | |
int main() | |
{ | |
init(); | |
bubbleSort(myArray); | |
printSorted(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment