Selection Sort
#include <stdio.h>
//int arr[] : input output
//batas : int
void selectionsort(int arr[],int batas)
{
int i;
int minindex;
int tukar;
int j;
for(i = 0;i<(batas-1);i++)
{
minindex = i;
for(j = i+1;j<batas;j++)
{
if(arr[minindex] > arr[j])
{
minindex = j;
}
}
tukar = arr[i];
arr[i] = arr[minindex];
arr[minindex] = tukar;
}
}
int main()
{
int arr[5] = {3,1,5,6,7}; //deklarasi array
selectionsort(arr,5);
int i;
for(i = 0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
Komentar
Posting Komentar