Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions 자료구조/Sort/daadaadaah/BubbleSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const swap = (array, index1, index2) => {
const temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}

const bubbleSort = (array) => {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - i -1 ; j++) {
if(array[j] > array[j+1]) {
swap(array, j, j+1);
}
}
}

return array;
}

test('BubbleSort', () => {
expect(bubbleSort([5, 2])).toEqual([2, 5]);

expect(bubbleSort([3, 5, 2])).toEqual([2, 3, 5]);

expect(bubbleSort([5, 2, 3, 4, 1,])).toEqual([1,2,3,4,5]);

});
45 changes: 45 additions & 0 deletions 자료구조/Sort/daadaadaah/InsertSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const getInsertIndex = (array, currentIndex) => {
let insertIndex = 0;

for (let j = currentIndex - 1; j >= 0; j--) {
if(array[j] < array[currentIndex]) {
insertIndex = j+1;
break;
}
}

return insertIndex;
}

const getTempInsertSortArray = (array, insertIndex, currentIndex) => {
const currentElement = array[currentIndex];

for(let k = currentIndex; k >= insertIndex + 1; k--) {
array[k] = array[k-1];
}

array[insertIndex] = currentElement;

return array;
}


const insertSort = (array) => {
for (let i = 1; i < array.length; i++) {
const currentIndex = i;

const insertIndex = getInsertIndex(array, currentIndex);

array = getTempInsertSortArray(array, insertIndex, currentIndex)
}

return array;
}

test('insertSort', () => {
expect(insertSort([5, 2])).toEqual([2, 5]);
expect(insertSort([3, 5, 2])).toEqual([2, 3, 5]);
expect(insertSort([5, 2, 3, 4, 1,])).toEqual([1,2,3,4,5]);
expect(insertSort([4, 7, 2, 3, 6, 1, 5, 8])).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);

});