11const getRandomInt = require ( "./helpers/getRandomInt" )
22
3+ const { performance } = require ( 'perf_hooks' ) ;
4+
35bubbleSort = ( array ) => {
46 for ( let i = 0 ; i < array . length ; i ++ ) {
57 for ( let j = 0 ; j < array . length - i - 1 ; j ++ ) {
@@ -24,7 +26,7 @@ insertionSort = (array) => {
2426 return array
2527}
2628
27- quickSortPartition = ( array , left = 0 , right = array . length - 1 ) => {
29+ /* quickSortPartition = (array, left = 0, right = array.length - 1) => {
2830 let pivot = array[Math.floor((right + left) / 2)]
2931 let low = left
3032 let high = right
@@ -57,7 +59,7 @@ quickSort = (array, left = 0, right = array.length - 1) => {
5759
5860 return array
5961}
60-
62+ */
6163merge = ( arr1 , arr2 ) => {
6264 let sorted = [ ] ;
6365
@@ -91,23 +93,27 @@ buildAarray = (n) => {
9193console . log ( "BUBBLE---------------------------" )
9294let bubbleArray = buildAarray ( 20 )
9395console . log ( bubbleArray )
96+ const t0 = performance . now ( ) ;
9497bubbleArray = bubbleSort ( bubbleArray )
98+ const t1 = performance . now ( ) ;
99+ console . log ( `Bubble sort: ${ t1 - t0 } milliseconds.` ) ;
95100console . log ( bubbleArray )
96101
97- /* let quickArray = buildAarray(20)
98- console.log(quickArray)
99- quickArray = quickSort(quickArray)
100- console.log(quickArray)
101- */
102102console . log ( "INSERTION---------------------------" )
103103let insertionArray = buildAarray ( 20 )
104104console . log ( insertionArray )
105- bubbleArray = insertionSort ( insertionArray )
105+ const t2 = performance . now ( ) ;
106+ insertionArray = insertionSort ( insertionArray )
107+ const t3 = performance . now ( ) ;
108+ console . log ( `Insertion sort: ${ t3 - t2 } milliseconds.` ) ;
106109console . log ( insertionArray )
107110
108111console . log ( "MERGE---------------------------" )
109112let mergeArray = buildAarray ( 20 )
110113console . log ( mergeArray )
114+ const t4 = performance . now ( ) ;
111115mergeArray = mergeSort ( mergeArray )
116+ const t5 = performance . now ( ) ;
117+ console . log ( `Merge sort: ${ t5 - t4 } milliseconds.` ) ;
112118console . log ( mergeArray )
113119
0 commit comments