Created
December 23, 2020 12:27
-
-
Save defrindr/a16c09f5dd984b3578dd1c7260e95b03 to your computer and use it in GitHub Desktop.
Materi Sorting Logika Dan Algoritma
This file contains hidden or 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
array = [30012, 123242,4395,123243935,1232]; | |
// bubble sort | |
function sort(arr) { | |
let index_pertukaran = 0, index_komparasi = 0; | |
for(i = 0; i < arr.length-1; i++) { | |
let finish_step = false, i=0, j=1, lebih_besar = false; | |
// step iterasi | |
while(finish_step == false){ | |
if(arr[i] > arr[j]){ | |
lebih_besar = true; | |
arr = swap(arr, i, j); | |
index_pertukaran++; | |
console.log(`Pertukaran ke-${index_pertukaran} = ${arr}`); | |
} | |
index_komparasi++; | |
console.log(`Komparasi ke-${index_komparasi} = ${arr}`); | |
// maju 1 step | |
i = j; | |
j = i+1; | |
if( i > arr.length || j > arr.length){ | |
finish_step = true; | |
} | |
} | |
} | |
console.log(arr); | |
} | |
function swap(arr, i, j) { | |
tmp = arr[j] | |
arr[j] = arr[i]; | |
arr[i] = tmp; | |
return arr; | |
} | |
sort(array); |
This file contains hidden or 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
array = [30012, 123242,4395,123243935,1232]; | |
// insertion sort | |
function sort(arr) { | |
let index_pertukaran = 0, index_komparasi = 0; | |
for(i = 1; i < arr.length; i++) { | |
tidak_lolos_validasi = true; | |
if(arr[i-1] > arr[i]){ | |
for(j = 0; j <= i; j++) { | |
if(arr[j] > arr[i]){ | |
arr = swap(arr, i, j); | |
index_pertukaran++; | |
console.log(`Pertukaran ke-${index_pertukaran} = ${arr}`); | |
} | |
index_komparasi++; | |
console.log(`Komparasi ke-${index_komparasi} = ${arr}`); | |
} | |
tidak_lolos_validasi = false; | |
} | |
if(tidak_lolos_validasi) { | |
index_komparasi++; | |
console.log(`Komparasi ke-${index_komparasi} = ${arr}`); | |
} | |
} | |
console.log(arr); | |
} | |
function swap(arr, i, j) { | |
tmp = arr[j] | |
arr[j] = arr[i]; | |
arr[i] = tmp; | |
return arr; | |
} | |
sort(array); |
This file contains hidden or 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
array = [30012, 123242,4395,123243935,1232]; | |
// selection sort | |
function sort(arr) { | |
let index_pertukaran = 0, index_komparasi = 0; | |
for(i = 0; i < arr.length-1; i++) { | |
min = i; | |
for(j = i + 1; j <= arr.length-1; j++) { | |
if (arr[j] < arr[min]) { | |
min = j; | |
index_pertukaran++; | |
console.log(`Pertukaran ke-${index_pertukaran} = ${arr}`); | |
} | |
index_komparasi++; | |
console.log(`Komparasi ke-${index_komparasi} = ${arr}`); | |
} | |
arr = swap(arr, i, min); | |
} | |
} | |
function swap(arr, i, j) { | |
tmp = arr[j] | |
arr[j] = arr[i]; | |
arr[i] = tmp; | |
return arr; | |
} | |
sort(array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment