Bubble sort using TypeScript

Share this video with your friends

Send Tweet

Bubble sort is considered the simplest sorting algorithm to implement. We’ll approach the problem in two ways. Once using a set number of iterations, and next using an idiomatic approach.

We’ll be covering this problem using TypeScript / Javascript

Brent Mitchell
Brent Mitchell
~ 7 years ago

I was wondering if you could help me understand how the swap line works.

[array[j], array[j + 1]] = [array[j + 1], array[j]];

Instead of swapping, it looks like the line is setting a new two item array to another new two item array.

Vishwas Chouhan
Vishwas Chouhan
~ 7 years ago

@brent, if you see the the array[j] and array[j + 1] are indexes in the array and we are just swapping the values at the index.

Vishwas Chouhan
Vishwas Chouhan
~ 7 years ago

@Basarat, how does the code get out of while (true) loop in the Idiomatic implementation?

Vishwas Chouhan
Vishwas Chouhan
~ 7 years ago

@Basarat, never mind I got it. Didn't realize the flag set was inside the while loop.

Chris Villanueva
Chris Villanueva
~ 7 years ago

@Basarat: I created arrays of 10K and 50K random integers. I sorted the same array in nodejs using both bubble sort examples. The idiomatic example performs slower that the basic sort example. Is that expected?

##############################################
#                                            #
           ***** bubble sort *****
#                                            #
##############################################
bubbleSortProcessing-[10000]: 4758.006ms
earlyTerminationBubbleSortProcessing-[10000]: 4994.364ms

[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node ./index.js`

##############################################
#                                            #
           ***** bubble sort *****
#                                            #
##############################################
bubbleSortProcessing-[50000]: 119734.447ms
earlyTerminationBubbleSortProcessing-[50000]: 125780.788ms

[nodemon] clean exit - waiting for changes before restart
 

I did notice a performance gain with Idiomatic sort using smaller data sets.

##############################################
#                                            #
           ***** bubble sort *****
#                                            #
##############################################
bubbleSortProcessing-[500]: 16.692ms
earlyTerminationBubbleSortProcessing-[500]: 12.605ms
[nodemon] clean exit - waiting for changes before restart
James Mulholland
James Mulholland
~ 5 years ago

I was wondering if you could help me understand how the swap line works.

[array[j], array[j + 1]] = [array[j + 1], array[j]];

Instead of swapping, it looks like the line is setting a new two item array to another new two item array.

^ For anyone wondering about this too, check out this SO answer https://stackoverflow.com/questions/872310/javascript-swap-array-elements

This is using array destructuring to swap elements. I'd seen it with variables, but not on arrays like this before.