diff --git "a/\354\236\220\353\243\214\352\265\254\354\241\260/Sort/daadaadaah/BubbleSort.test.js" "b/\354\236\220\353\243\214\352\265\254\354\241\260/Sort/daadaadaah/BubbleSort.test.js" new file mode 100644 index 0000000..7aa8b53 --- /dev/null +++ "b/\354\236\220\353\243\214\352\265\254\354\241\260/Sort/daadaadaah/BubbleSort.test.js" @@ -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]); + +}); diff --git "a/\354\236\220\353\243\214\352\265\254\354\241\260/Sort/daadaadaah/InsertSort.test.js" "b/\354\236\220\353\243\214\352\265\254\354\241\260/Sort/daadaadaah/InsertSort.test.js" new file mode 100644 index 0000000..8a76c24 --- /dev/null +++ "b/\354\236\220\353\243\214\352\265\254\354\241\260/Sort/daadaadaah/InsertSort.test.js" @@ -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]); + +});