Last active
April 24, 2021 08:32
-
-
Save Changsik00/b7ef13d1492fd19846f92d2d6da2ee2a to your computer and use it in GitHub Desktop.
이것은 내가 공부하면서 정리한 sort 내용들..
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
function swap(array, a, b) { | |
const temp = array[a] | |
array[a] = array[b] | |
array[b] = temp | |
} | |
function bubbleSort(array) { | |
let i , j, temp = null | |
const length = array.length | |
for(i = 0; i < length - 1; i++) { | |
for(j = 0; j < length - 1 - i; j++) { | |
if(array[j] > array[j + 1]) { | |
swap(array, j, j + 1) | |
} | |
} | |
} | |
return array | |
} | |
function selectSort(array) { | |
let i , j, min, temp | |
const length = array.length | |
for(i = 0; i < length -1; i++) { | |
min = i | |
for(j = i + 1; j < length; j++) { | |
if(array[j] < array[min]) { | |
swap(array, j, min) | |
} | |
} | |
} | |
return array | |
} | |
function insertionSort(array) { | |
let i , j, temp | |
const length = array.length | |
for(i = 0; i < length -1; i++) { | |
if(array[i] > array[i + 1]) { | |
for(j = i; j >= 0; j--) { | |
if(array[j] > array[j+ 1]) { | |
swap(array, j, j+ 1) | |
} | |
} | |
} | |
} | |
return array | |
} | |
function quickSort(array, start, end) { | |
if(start >= end) return; | |
let pivot, left = start, right = end | |
pivot = array[parseInt((start + end) / 2)] | |
while(left <= right) { | |
while(array[right] > pivot) right-- | |
while(array[left] < pivot) left++ | |
if(left <=right) | |
{ | |
swap(array, left, right) | |
left++; right--; | |
} | |
} | |
quickSort(array, start, left - 1) | |
quickSort(array, left, end ) | |
return array | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment