#include<stdio.h>
void selectionsort();
void insertionsort();
int main(){
selectionsort();
insertionsort();
return 0;
}
void selectionsort() {
int i,j,N,arr[20];
printf("masukkan jumlah element untuk selectionsort: ");
scanf("%d",&N);
printf("Masukan %d element: \n",N);
for(i=0;i<N;i++){
scanf("%d",&arr[i]);}
for (i = 0; i < N; i++) {
int min = arr[i];
int pos = i;
for (j = i; j < N; j++) {
/* Cari nilai yang terkecil */
if (arr[j] < min) {
min = arr[j];
pos = j;
}
}
/* Tukar nilai terkecil ke arr[i] jika pos tdk sama i */
if(i!=pos) {
int temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
}
printf("hasil selectionsort: ");
for(i=0;i<N;i++){
printf(" %d",arr[i]);}
printf("\n");
}
void insertionsort() {
int i,j,N,arr[20];
printf("masukkan jumlah element untuk insertionsort: ");
scanf("%d",&N);
printf("Masukan %d element: \n",N);
for(i=0;i<N;i++){
scanf("%d",&arr[i]);}
for (i = 1; i < N; i++) {
int temp = arr[i];
int pos = i;
for (j = i; j > 0; j--) {
if (temp < arr[j-1]) {
arr[j] = arr[j-1];
pos--;
}
}
arr[pos]=temp;
}
printf("hasil insertionsort: ");
for(i=0;i<N;i++){
printf(" %d",arr[i]);}
printf("\n");
}
void selectionsort();
void insertionsort();
int main(){
selectionsort();
insertionsort();
return 0;
}
void selectionsort() {
int i,j,N,arr[20];
printf("masukkan jumlah element untuk selectionsort: ");
scanf("%d",&N);
printf("Masukan %d element: \n",N);
for(i=0;i<N;i++){
scanf("%d",&arr[i]);}
for (i = 0; i < N; i++) {
int min = arr[i];
int pos = i;
for (j = i; j < N; j++) {
/* Cari nilai yang terkecil */
if (arr[j] < min) {
min = arr[j];
pos = j;
}
}
/* Tukar nilai terkecil ke arr[i] jika pos tdk sama i */
if(i!=pos) {
int temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
}
printf("hasil selectionsort: ");
for(i=0;i<N;i++){
printf(" %d",arr[i]);}
printf("\n");
}
void insertionsort() {
int i,j,N,arr[20];
printf("masukkan jumlah element untuk insertionsort: ");
scanf("%d",&N);
printf("Masukan %d element: \n",N);
for(i=0;i<N;i++){
scanf("%d",&arr[i]);}
for (i = 1; i < N; i++) {
int temp = arr[i];
int pos = i;
for (j = i; j > 0; j--) {
if (temp < arr[j-1]) {
arr[j] = arr[j-1];
pos--;
}
}
arr[pos]=temp;
}
printf("hasil insertionsort: ");
for(i=0;i<N;i++){
printf(" %d",arr[i]);}
printf("\n");
}
Categories:
blog,
strukturdata