Created
June 13, 2021 10:12
-
-
Save defrindr/3e0b18e9e896319e314e0c4c01e62eef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<time.h> | |
#define MAX 100 | |
#define SORT_ASC 1 | |
#define SORT_DESC 0 | |
void Merge(int arr[], int left, int median, int right,int sorttype) { | |
int temp[MAX]; | |
int kiri1, kanan1, kiri2, kanan2, i, j; | |
kiri1 = left; | |
kanan1 = median; | |
kiri2 = median + 1; | |
kanan2 = right; | |
i = left; | |
while ((kiri1 <= kanan1) && (kiri2 <= kanan2)) { | |
if(sorttype){ | |
if (arr[kiri1] <= arr[kiri2]) { | |
temp[i] = arr[kiri1]; | |
kiri1++; | |
} else { | |
temp[i] = arr[kiri2]; | |
kiri2++; | |
} | |
}else{ | |
if (arr[kiri1] >= arr[kiri2]) { | |
temp[i] = arr[kiri1]; | |
kiri1++; | |
} else { | |
temp[i] = arr[kiri2]; | |
kiri2++; | |
} | |
} | |
i++; | |
} | |
while (kiri1 <= kanan1) { | |
temp[i] = arr[kiri1]; | |
kiri1++; | |
i++; | |
} | |
while (kiri2 <= kanan2) { | |
temp[i] = arr[kiri2]; | |
kiri2++; | |
i++; | |
} | |
j = left; | |
while (j <= right) { | |
arr[j] = temp[j]; | |
j++; | |
} | |
} | |
void MergeSort(int arr[], int l, int r,int sorttype) { | |
int med; | |
if (l < r) { | |
med = (l + r) / 2; | |
MergeSort(arr, l, med,sorttype); | |
MergeSort(arr, med + 1, r,sorttype); | |
Merge(arr, l, med, r,sorttype); | |
} | |
} | |
int Partition(int arr[], int p, int r, int sorttype) { | |
int i, j; | |
int pivot, temp; | |
pivot = arr[p]; //pivot pada index 0 | |
i = p; | |
j = r; | |
while (i <= j) { | |
if (sorttype) { | |
while (pivot < arr[j]) | |
j--; | |
while (pivot > arr[i]) | |
i++; | |
} else { | |
while (pivot > arr[j]) | |
j--; | |
while (pivot < arr[i]) | |
i++; | |
} | |
if (i < j) { | |
temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
j--; | |
i++; | |
} else | |
return j; | |
} | |
return j; | |
} | |
void QuickSort(int arr[], int p, int r, int sorttype) { | |
int q; | |
if (p < r) { | |
q = Partition(arr, p, r, sorttype); | |
QuickSort(arr, p, q, sorttype); | |
QuickSort(arr, q + 1, r, sorttype); | |
} | |
} | |
void BubbleSort(int arr[], int sorttype) { | |
int i, j, temp; | |
for (i = 0; i < MAX - 1; i++) { | |
for (j = 0; j < MAX - i - 1; j++) { | |
if(sorttype){ | |
if (arr[j] > arr[j + 1]) { | |
temp = arr[j + 1]; | |
arr[j + 1] = arr[j]; | |
arr[j] = temp; | |
} | |
}else{ | |
if (arr[j] < arr[j + 1]) { | |
temp = arr[j + 1]; | |
arr[j + 1] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
} | |
} | |
} | |
void ShellSort(int arr[], int sorttype) { | |
int i, jarak, temp; | |
int did_swap = 1; | |
jarak = MAX; | |
while (jarak > 1) { | |
jarak = jarak / 2; | |
did_swap = 1; | |
while (did_swap) { | |
did_swap = 0; | |
i = 0; | |
if (sorttype) { | |
while (i < (MAX - jarak)) { | |
if (arr[i] > arr[i + jarak]) { | |
temp = arr[i]; | |
arr[i] = arr[i + jarak]; | |
arr[i + jarak] = temp; | |
did_swap = 1; | |
} | |
i++; | |
} | |
} else { | |
while (i < (MAX - jarak)) { | |
if (arr[i] < arr[i + jarak]) { | |
temp = arr[i]; | |
arr[i] = arr[i + jarak]; | |
arr[i + jarak] = temp; | |
did_swap = 1; | |
} | |
i++; | |
} | |
} | |
} | |
} | |
} | |
void insertionSort(int * data[], int sorttype) { | |
int tmp, j, i; | |
for (j = 1; j < MAX; j++) { | |
i = j - 1; | |
tmp = data[j]; | |
if(sorttype){ | |
while (i >= 0 && tmp < data[i]) { | |
data[i + 1] = data[i]; | |
i--; | |
} | |
}else{ | |
while (i >= 0 && tmp > data[i]) { | |
data[i + 1] = data[i]; | |
i--; | |
} | |
} | |
data[i + 1] = tmp; | |
} | |
} | |
void swapValue(int *data[],int idx,int i,int tmp) { | |
tmp = data[idx]; | |
data[idx]=data[i]; | |
data[i] = tmp; | |
} | |
void selectionSort(int * data[],int sorttype) { | |
int tmp, i=0, j, idx,min; | |
while (i < MAX-1) { | |
idx=i; | |
min = data[i]; | |
for(j=i+1;j<MAX;j++) { | |
if(sorttype){ | |
if(data[j]<min){ | |
min=data[j]; | |
idx=j; | |
} | |
}else{ | |
if(data[j]>min){ | |
min=data[j]; | |
idx=j; | |
} | |
} | |
} | |
swapValue(data,idx,i,tmp); | |
i++; | |
} | |
} | |
void main(){ | |
int type,sort; | |
int data_awal[MAX], data_urut[MAX]; | |
int i; | |
long k1, k2; | |
printf("ALGORITMA SORTING\n"); | |
printf("1. Insertion\n2. Selection\n3. Bubble\n4. Shell\n5. Quick\n6.Merge\n"); | |
printf("Masukkan pilihan : "); | |
scanf("%d", &sort); | |
printf("1. Ascending\n2. Descending\n"); | |
printf("Pilihan sorting : "); | |
scanf("%d", &type); | |
// generate random data | |
printf("\nSebelum pengurutan : \n"); | |
for (i = 0; i < MAX; i++) { | |
srand(time(NULL) * (i + 1)); | |
data_awal[i] = rand() % 100 + 1; | |
printf("%d ", data_awal[i]); | |
} | |
for (i = 0; i < MAX; i++) | |
data_urut[i] = data_awal[i]; | |
printf("\n\nPengurutan "); | |
switch (sort) | |
{ | |
case 1: | |
printf("Insertion "); | |
if(type==1){ | |
printf("ASC :\n"); | |
insertionSort(data_urut,SORT_ASC); | |
}else{ | |
printf("DESC :\n"); | |
insertionSort(data_urut,SORT_DESC); | |
} | |
break; | |
case 2: | |
printf("Selection "); | |
if(type==1){ | |
printf("ASC :\n"); | |
selectionSort(data_urut,SORT_ASC); | |
}else{ | |
printf("DESC :\n"); | |
selectionSort(data_urut,SORT_DESC); | |
} | |
break; | |
case 3: | |
printf("Bubble "); | |
if(type==1){ | |
printf("ASC :\n"); | |
BubbleSort(data_urut,SORT_ASC); | |
}else{ | |
printf("DESC :\n"); | |
BubbleSort(data_urut,SORT_DESC); | |
} | |
break; | |
case 4: | |
printf("Shell "); | |
if(type==1){ | |
printf("ASC :\n"); | |
ShellSort(data_urut,SORT_ASC); | |
}else{ | |
printf("DESC :\n"); | |
ShellSort(data_urut,SORT_DESC); | |
} | |
break; | |
case 5: | |
printf("Quick "); | |
if(type==1){ | |
printf("ASC :\n"); | |
QuickSort(data_urut,0,MAX-1,SORT_ASC); | |
}else{ | |
printf("DESC :\n"); | |
QuickSort(data_urut,0,MAX-1,SORT_DESC); | |
} | |
break; | |
case 6: | |
printf("Merge "); | |
if(type==1){ | |
printf("ASC :\n"); | |
MergeSort(data_urut,0,MAX-1,SORT_ASC); | |
}else{ | |
printf("DESC :\n"); | |
MergeSort(data_urut,0,MAX-1,SORT_DESC); | |
} | |
break; | |
default: | |
printf("\n\n=========Terminated=========="); | |
exit(1); | |
} | |
for (i = 0; i < MAX; i++) | |
printf("%d ", data_urut[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment