Loading...
Tuesday, December 24, 2013

Algoritma Bubble Sort pada C++

#include<stdio.h>
//Sorting Ascending dengan Bubble Sort

int main()
{
int jum;
printf("Masukan batasan Array: ");
scanf("%d",&jum);

int M[jum];
for (int i=0; i<jum; i++)
{
printf("Masukan Nilai Array: ");
scanf("%d",&M[i]);
}

printf("\nArray Input: \n");
for (int i=0; i<jum; i++)
{
printf("%i ",M[i]);
}

printf("\n\nArray Setelah diurutkan: \n");
int tmp;
for (int i=0;i<=(jum-1);i++)
{
for(int j=jum-1;j>=(i+1);j--)
{
if(M[j]<M[j-1])
{
tmp = M[j];
M[j]=M[j-1];
M[j-1]=tmp;
}
}
}
for (int i=0; i<jum; i++)
{
printf("%i ",M[i]);
}
}

Output Program

0 comments:

Post a Comment

 
TOP