Skip to content

Instantly share code, notes, and snippets.

@muhammedeminoglu
Last active December 2, 2022 17:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhammedeminoglu/c69cabee84fd2d1fb3dcceb0d09344b7 to your computer and use it in GitHub Desktop.
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
#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