Last active
May 4, 2021 14:55
-
-
Save defrindr/ce24ff0a3f8d9d9b5ace7b0745eb4d00 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> | |
int n = 0; | |
void sortDesc(int * data[]) { | |
int tmp, j, i; | |
for (j = 1; j < n; j++) { | |
i = j - 1; | |
tmp = data[j]; | |
while (i >= 0 && tmp > data[i]) { | |
data[i + 1] = data[i]; | |
i--; | |
} | |
data[i + 1] = tmp; | |
} | |
} | |
void print(int * data[]) { | |
int j; | |
for (j = 0; j < n; j++) { | |
printf("%d,", data[j]); | |
} | |
printf("\n"); | |
} | |
void init_random() { | |
time_t t; | |
srand((unsigned) time( & t)); | |
} | |
int gRandom(int max) { | |
return (rand() % max); | |
} | |
void acakData(int * data[]) { | |
int i; | |
for (i = 0; i < n; i++) | |
data[i] = gRandom(n); | |
} | |
long toSecond(msec){ | |
return msec; | |
} | |
int main() { | |
clock_t a1, a2,c1,c2; | |
init_random(); | |
n = gRandom(1000); | |
// n=200000; | |
a1=clock(); | |
int * data[n]; | |
acakData(data); | |
a2=clock(); | |
printf("Jumlah Data: %d\n",n); | |
printf("Data Awal:\n"); | |
printf("Lama waktu: %ld\n",toSecond(a2-a1)); | |
print(data); | |
c1=clock(); | |
sortDesc(data); | |
c2=clock(); | |
printf("SORT DESC:\n"); | |
printf("Lama waktu: %ld\n",toSecond(c2-c1)); | |
print(data); | |
} | |
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> | |
int n = 0; | |
void swapValue(int *data[],int idx,int i,int tmp) { | |
tmp = data[idx]; | |
data[idx]=data[i]; | |
data[i] = tmp; | |
} | |
void sortDesc(int * data[]) { | |
int tmp, i=0, j, idx,min; | |
while (i < n-1) { | |
idx=i; | |
min = data[i]; | |
for(j=i+1;j<n;j++) { | |
if(data[j]>min){ | |
min=data[j]; | |
idx=j; | |
} | |
} | |
swapValue(data,idx,i,tmp); | |
i++; | |
} | |
} | |
void print(int * data[]) { | |
int j; | |
for (j = 0; j < n; j++) { | |
printf("%d,", data[j]); | |
} | |
printf("\n"); | |
} | |
void init_random() { | |
time_t t; | |
srand((unsigned) time( & t)); | |
} | |
int gRandom(int max) { | |
return (rand() % max); | |
} | |
void acakData(int * data[]) { | |
int i; | |
for (i = 0; i < n; i++) | |
data[i] = gRandom(n); | |
} | |
long toSecond(msec){ | |
return msec; | |
} | |
int main() { | |
clock_t a1, a2,c1,c2; | |
init_random(); | |
n = gRandom(20); | |
// n=200000; | |
a1=clock(); | |
int * data[n]; | |
acakData(data); | |
a2=clock(); | |
printf("Jumlah Data: %d\n",n); | |
printf("Data Awal:\n"); | |
printf("Lama waktu: %ld\n",toSecond(a2-a1)); | |
print(data); | |
c1=clock(); | |
sortDesc(data); | |
c2=clock(); | |
printf("SORT DESC:\n"); | |
printf("Lama waktu: %ld\n",toSecond(c2-c1)); | |
print(data); | |
} |
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> | |
#define SORT_ASC 0 | |
#define SORT_DESC 1 | |
#define checkCondition(sort, tmp, data)(sort) ? tmp < data : tmp > data | |
int n = 0; | |
void sort(int * data[], int sort) { | |
int tmp, j, i, condition, sort_condition; | |
sort_condition = SORT_ASC == sort; | |
for (j = 1; j < n; j++) { | |
i = j - 1; | |
tmp = data[j]; | |
condition = checkCondition(sort_condition, tmp, data[i]); | |
while (i >= 0 && condition) { | |
data[i + 1] = data[i]; | |
i--; | |
// ubah kondisi dalam while | |
condition = checkCondition(sort_condition, tmp, data[i]); | |
} | |
data[i + 1] = tmp; | |
} | |
} | |
void print(int * data[]) { | |
int j; | |
for (j = 0; j < n; j++) { | |
printf("%d,", data[j]); | |
} | |
printf("\n"); | |
} | |
void init_random() { | |
time_t t; | |
srand((unsigned) time( & t)); | |
} | |
int gRandom(int max) { | |
return (rand() % max); | |
} | |
void acakData(int * data[]) { | |
int i; | |
for (i = 0; i < n; i++) | |
data[i] = gRandom(100); | |
} | |
int main() { | |
init_random(); | |
n = gRandom(20); | |
int * data[n]; | |
acakData(data); | |
printf("Data Awal:\n"); | |
print(data); | |
sort(data, SORT_ASC); | |
printf("SORT ASC:\n"); | |
print(data); | |
sort(data, SORT_DESC); | |
printf("SORT DESC:\n"); | |
print(data); | |
} |
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 SORT_ASC 0 | |
#define SORT_DESC 1 | |
#define checkCondition(sort, data, min)(sort) ? data < min : data > min | |
int n = 0; | |
void swapValue(int * data[], int idx, int i, int tmp) { | |
tmp = data[idx]; | |
data[idx] = data[i]; | |
data[i] = tmp; | |
} | |
void sort(int * data[], int sort) { | |
int tmp, i = 0, j, idx, min, | |
//membalik dari kanan ke kiri | |
sort_type = SORT_DESC == sort; | |
while (i < n - 1) { | |
idx = i; | |
min = data[i]; | |
for (j = i + 1; j < n; j++) { | |
if (checkCondition(sort_type, data[j], min)) { | |
min = data[j]; | |
idx = j; | |
} | |
} | |
swapValue(data, idx, i, tmp); | |
i++; | |
} | |
} | |
void print(int * data[]) { | |
int j; | |
for (j = 0; j < n; j++) { | |
printf("%d,", data[j]); | |
} | |
printf("\n"); | |
} | |
void init_random() { | |
time_t t; | |
srand((unsigned) time( & t)); | |
} | |
int gRandom(int max) { | |
return (rand() % max); | |
} | |
void acakData(int * data[]) { | |
int i; | |
for (i = 0; i < n; i++) | |
data[i] = gRandom(n); | |
} | |
int main() { | |
init_random(); | |
n = gRandom(50); | |
int * data[n]; | |
acakData(data); | |
printf("Data Awal:\n"); | |
print(data); | |
sort(data, SORT_ASC); | |
printf("SORT ASC:\n"); | |
print(data); | |
sort(data, SORT_DESC); | |
printf("SORT DESC:\n"); | |
print(data); | |
} |
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 20 | |
void main() { | |
int data_awal[MAX], data_urut[MAX]; | |
int i; | |
printf("Sebelum pengurutan : \n"); | |
for (i = 0; i < MAX; i++) { | |
srand(time(NULL) * (i + 1)); | |
data_awal[i] = rand() % 100 + 1; | |
printf("%d ", data_awal[i]); | |
} | |
printf("\n"); | |
for (i = 0; i < MAX; i++) | |
data_urut[i] = data_awal[i]; | |
} |
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 20 | |
void InsertionSort(int arr[]) { | |
int i, j, key; | |
for (i = 1; i < MAX; i++) { | |
key = arr[i]; | |
j = i - 1; | |
while (j >= 0 && arr[j] > key) { | |
arr[j + 1] = arr[j]; | |
j--; | |
} | |
arr[j + 1] = key; | |
} | |
} | |
void main() { | |
int data_awal[MAX], data_urut[MAX]; | |
int i; | |
long k1, k2; | |
printf("Sebelum pengurutan : \n"); | |
for (i = 0; i < MAX; i++) { | |
srand(time(NULL) * (i + 1)); | |
data_awal[i] = rand() % 100 + 1; | |
printf("%d ", data_awal[i]); | |
} | |
printf("\nSetelah pengurutan : \n"); | |
for (i = 0; i < MAX; i++) | |
data_urut[i] = data_awal[i]; | |
time( & k1); | |
InsertionSort(data_urut); | |
time( & k2); | |
for (i = 0; i < MAX; i++) | |
printf("%d ", data_urut[i]); | |
printf("\nWaktu = %ld\n", k2 - k1); | |
} |
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 20 | |
void SelectionSort(int arr[]) { | |
int i, j, temp, min, min_id; | |
i = 0; | |
while (i < MAX - 1) { | |
min_id = i; | |
min = arr[i]; | |
for (j = i + 1; j < MAX; j++) | |
if (arr[j] < min) { | |
min = arr[j]; | |
min_id = j; | |
} | |
temp = arr[min_id]; | |
arr[min_id] = arr[i]; | |
arr[i] = temp; | |
i++; | |
} | |
} | |
void main() { | |
int data_awal[MAX], data_urut[MAX]; | |
int i; | |
long k1, k2; | |
printf("Sebelum pengurutan : \n"); | |
for (i = 0; i < MAX; i++) { | |
srand(time(NULL) * (i + 1)); | |
data_awal[i] = rand() % 100 + 1; | |
printf("%d ", data_awal[i]); | |
} | |
printf("\nSetelah pengurutan : \n"); | |
for (i = 0; i < MAX; i++) | |
data_urut[i] = data_awal[i]; | |
time( & k1); | |
SelectionSort(data_urut); | |
time( & k2); | |
for (i = 0; i < MAX; i++) | |
printf("%d ", data_urut[i]); | |
printf("\nWaktu = %ld\n", k2 - k1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment