Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
52 views

Sorting Algorithms Notes

Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. Quick sort works by choosing a pivot element and partitioning the list into sublists based on whether elements are less than or greater than the pivot, then applying the same approach recursively to the sublists. Shell sort works by sorting sublists of widely spaced elements then progressively narrowing the spacing to fully sort the list, using shuttle sort on each sublist.

Uploaded by

Prakash Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Sorting Algorithms Notes

Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. Quick sort works by choosing a pivot element and partitioning the list into sublists based on whether elements are less than or greater than the pivot, then applying the same approach recursively to the sublists. Shell sort works by sorting sublists of widely spaced elements then progressively narrowing the spacing to fully sort the list, using shuttle sort on each sublist.

Uploaded by

Prakash Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Sorting Algorithms 1

These algorithms sort a list into ascending or descending order. It is


important to remember that it is showing the right method that is
important, not just the final result.

Bubble Sort:
Compare the first and second numbers, and swap them if they are in the
wrong order. Now do the 2nd and 3rd. Continue to the end of the list. The
last number will then be in the right place. This is called the FIRST
PASS. Now start again at the beginning of the list and repeat the process
(but this time you do not need to compare the last number). This will fix
the second to last number in place, and is called the second pass.
Continue with this process until either all the numbers are fixed, or you
have a pass with no swaps.

Quick Sort:
Choose the first number to be the pivot. Place the numbers that are
smaller than the pivot before it, and the numbers that are bigger than the
pivot after it without changing their order. The pivot is now fixed in
place. For each sublist, choose the first number to be the pivot, and repeat
the process. A number is fixed in place if either it has been a pivot or if it
is in a sublist by itself. Continue until all the numbers are fixed in place.
Sorting Algorithms 2

These algorithms sort a list into ascending or descending order. It is


important to remember that it is showing the right method that is
important, not just the final result.

Shuttle Sort:
Compare the first and second numbers and swap them if they are in the
wrong order – this is the first pass. Now compare the 2nd and 3rd, and
swap them if they are in the wrong order. If you had to swap these,
compare the new 2nd and 1st, and swap these if they are in the wrong
order. This is the 2nd pass. Now do the 3rd and 4th (and if necessary the 3rd
and 2nd, and the 2nd and 1st). Continue until you have done the whole list.

Shell Sort:
Divide the number of items in the list by 2, and ignore any remainder.
Divide the list into this many sublists; you do this by putting the first item
in the first sublist, the 2nd item in the 2nd sublist etc. When you have 1
item in each sublist, the next item goes in the 1st sublist. Continue until all
items are in a sublist (so the first sublist will have one more item than the
others if there was a remainder).
Shuttle sort these sublists.
Now divide this number of sublists by 2 (ignoring any remainder) and
divide the list into this many sublists.
Shuttle sort these sublists.
Repeat this process until you just have 1 “sublist” (obviously this is the
whole list) which you shuttle sort.
1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7
7 6 5 4 3 2 1

7 6 5 4 3 2 1

7 6 5 4 3 2 1

7 6 5 4 3 2 1

7 6 5 4 3 2 1

7 6 5 4 3 2 1

7 6 5 4 3 2 1

7 6 5 4 3 2 1

You might also like