Chapter 1 Algorithms : Sorting Algo Flashcards

1
Q

How to do QUICK SORT

A

1) circle the pivot which is most left number in each sub list
2) if ascending descending , put all the numbers lower to the left and higher to the right of the sub list but the ORDER OF THIS MUST BE MAINTAINED EACH TIME
3) once this done, BOX THE PIVOT to let exmainer know its been used snd ordered

4) now continue for the new sublists made until ALL THE PIVOTS HAVE BEEN BOXED

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to count the number of COMPARISONS MADE each time for quick sort

What about sublists of 1 , just the pivot ?

A

Just count the number of un pivot and un boxed numbers each row , this is equal to number of comparisons

But just count them , pivot compared with 1,2 etc

SUBLISTS OF 1 HAVE NOTHING TO COMPARE TO SO THEY JUST GET BOXED UP WITH 0 COMPARISONS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s worse case scenarios for the quick sort and thus how to find complexity

A

Worse case = reverse / already in order

This gives the comparisons 4,3,2,1 which gives triangle numbers

However this must be adjusted one back , giving 1/2n (n-1)

Still QUADRATIC COMPLEXITY HOWEVER

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is n when doing the nth term for complexity, basic,sly how many passes must be made to ensure all ordered

A

N = number of items to be ordered snd number of passed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to do bubble sort

A

Compare first two, which ever bigger pushed ti right, and then this continues happening with the next two items

By the end of the first pass, the BIGGEST NUMBER SHOULD HAVE BUBBLED TO THE TOP

The bubbled ti the top, MAKE SURE TO CAP IT AND IGNORE IT FOR THE NEXT ONE

End when NO MORE SWAPS HAVE BEEN MADE
- however that line still counts for comparisons!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many comparisons made in bubble sort for each pass?
Thus what’s its complexity

A

Each pass the min number of comparisons made must be one less than the number of in the list
(So first pass with 8 elements = 7 comparisons must be made)

Thus it follows it goes 5,4,3 2 1 etc

Thus triangle numbers aagain = order of n2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

I’d doing descending order, how to adjust bubble sort

A

Just make biggest for to left, in this case the SMALEEST WILL BUBBLE TO LEFT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Remember if there are n terms, for bubble and quick sort there will be n passes

A

However on the last pass maybe no swap !

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to the shuttle sort

A

First put a line between first rwo numbers
- then check if the number closest to line is bigger or smaller than one on left
- if match condition then swap. Keep doing this until NO MORE SWAPS occur = right position

Record this

Now for next pass, make line one unit BIGGER and repeat

End when the line can no longer move go next one up = sorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Remember a comparison is still made if no swap happens!

A

So if jtsbigger it still has to check left tk see , don’t forget !

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Shuttle sort complexity?

A

Check the Worde case = reverse ordered

Find its triangle numbers = n2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly