Skip to content

Instantly share code, notes, and snippets.

@shaunlgs
Created October 9, 2016 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunlgs/0ad5fb8d583de0553a50e79c64630ba0 to your computer and use it in GitHub Desktop.
Save shaunlgs/0ad5fb8d583de0553a50e79c64630ba0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <conio.h>
void BubbleSort(int [], int);
int main()
{
int i,saiz=10, susun[10] = {24,64,10,5,6,31,15,20,56,34};
printf("\n Data belum terisih :\n");
for (i=0;i<10;i++)
printf("%3d",susun[i]);
BubbleSort(susun, saiz);
printf("\n Data telah terisih mengikut turutan menaik :\n");
for (i=0;i<10;i++)
printf("%3d",susun[i]);
getch();
return 0;
}
void BubbleSort(int data[], int listSize)
{ int pass, tempValue;
for (pass =1; pass<listSize; pass++ )
{
// moves the largest element to the
// end of the array
for (int x = 0; x < listSize - pass; x++)
//compare adjacent elements
if ( data[x]>data[x+1] )
{ // swap elements
tempValue = data[x];
data[x] = data[x+1];
data[x+1] = tempValue;
}
}
} // end Bubble Sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment