This presentation includes custom animations.
description
Transcript of This presentation includes custom animations.
This presentation includes custom animations.
To view the animations, you must view the presentation in Slide Show modeand activeX controls must be allowed.
If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode.
If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon
A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.
Sorting Algorithms
Christine S. WolfeOhio University Lancaster2008-Aug-01
This lesson describes two famous sorting algorithms.
Vocabulary
ascending orderbubble sortdescending orderselection sortsort
To sort a list of items means to organize the list based on some order such as alphabetical order or numeric order.
When a list is sorted with the lowest value item at the top, the list is said to be in ascending order.
When a list is sorted with the highest value items at the top, the list is said to be in descending order.
169
961
How do I sort thee? Let me count the ways.
Bubble sort
Insertion sort
Selection sort
Shell sort
Heap sort
Merge sort
Quick sort
A Few Famous Sorting Algorithms
Simple and "Slow"
Complex and "Fast"
The selection sort works by hunting for the value that belongs in a position.
Round 1
EndStart
30
10
60
20
30
10
60
20
40 40
50 50
The selection sort works by hunting for the value that belongs in a position.
Round 2
EndStart
30
10
60
20
30
10
60
20
4040
5050
The selection sort works by hunting for the value that belongs in a position.
Round 3
EndStart
30
10
60
20
40
50
30
10
60
20
40
50
The selection sort works by hunting for the value that belongs in a position.
Round 4
EndStart
30
10
60
20
40
50
30
10
60
20
40
50
The selection sort works by hunting for the value that belongs in a position.
Round 5
EndStart
30
10
60
20
40
50
30
10
60
20
40
50
Round 1 Round 2 Round 3 Round 4 Round 5
Notice that at the end of each round, another value is in the correct position.
If the numbers are stored in an array named, Box, how is the array declared? int Box[6];
Round 1 Round 2 Round 3
Round 4 Round 5
Click for Answer
for (FillMe = 0; FillMe <= 5; ++FillMe){
}
int FillMe;int Box[6];
How does the algorithm loop through all the positions it has to fill?
Round 1 Round 2 Round 3
Round 4 Round 5
Click for Answer
for (FillMe = 0; FillMe <= 5; ++FillMe)
LowValue = Box[FillMe];{
for (test = FillMe + 1; test <= 4; ++test)
{if (Box[test] < LowValue)
{LowValue = Box[test];LowIndex = test;
LowIndex = FillMe;
}}
}
int FillMe;int LowValue;
int LowIndex;
int Box[6];
How does the algorithm find the value to store in the target position?
Round 1 Round 2 Round 3
Round 4 Round 5
Click for Answer
for (FillMe = 0; FillMe <= 5; ++FillMe)
LowValue = Box[FillMe];{
for (test = FillMe + 1; test <= 5; ++test)
{if (Box[test] < LowValue)
{LowValue = Box[test];LowIndex = test;
LowIndex = FillMe;
}}
if (FillMe != LowIndex)
SwapValue = Box[FillMe];Box[FillMe] = Box[LowIndex]Box[LowIndex] = SwapValue;
{
}}
int FillMe;int LowValue;int SwapValue;int LowIndex;
int Box[6];
How does the algorithm store the value in the target position without losing the value that was there originally?
Round 1 Round 2 Round 3
Round 4 Round 5
Click for Answer
for (FillMe = 0; FillMe <=5; ++FillMe)
LowValue = Box[FillMe];{
for (test = FillMe + 1; test <= 4; ++test)
{if (Box[test] < LowValue)
{LowValue = Box[test];LowIndex = test;
LowIndex = FillMe;
}}
if (FillMe != LowIndex)
SwapValue = Box[FillMe];Box[FillMe] = Box[LowIndex]Box[LowIndex] = SwapValue;
{
}}
int FillMe;int LowValue;int SwapValue;int LowIndex;
int Box[6];
Be sure you understand this algorithm completely.
Round 1 Round 2 Round 3
Round 4 Round 5
3
2
4
5
1
3
2
4
5
1
3
2
4
5
1
3
2
4
5
1
3
2
4
5
1
3
2
4
5
1
The bubble sort works by moving the low values to the top while pushing the high values to the bottom.
Round 1
After compare
1
After compare
2
After compare
3
After compare
4Start
3
2
4
5
1
3
2
4
5
1
3
2
4
5
1
The bubble sort works by moving the low values to the top while pushing the high values to the bottom.
Round 2
After compare
1
After compare
2
After compare
3Start
3
2
4
5
1
3
2
4
5
1 3
2
4
5
1
The bubble sort works by moving the low values to the top while pushing the high values to the bottom.
Round 3
After compare
1
After compare
2Start
3
2
4
5
1
3
2
4
5
1
The bubble sort works by moving the low values to the top while pushing the high values to the bottom.
Round 4
After compare
1Start
3
2
4
5
1
How does the code declare the array?
Click for Answer
Assume the bubbles are in an array of ints named Bubble.
int Bubble[5];Round 1 Round 2
Round 4Round 3
Declare the array with 5 elements because that is how many numbers are being stored.
How does the algorithm swap 2 values? Click for Answer
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1]Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
Round 1 Round 2
Round 4Round 3
A holding variable holds one of the values so it isn't overrun by the new value.
Round 1 Round 2
Round 4Round 3
How does the algorithm determine if it should swap the 2 values?
Click for Answer
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1]Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
if (Bubble[test] > Bubble[test + 1])
{
}
Notice that the element number can be the result of a calculation [test + 1] as long as it's an int.
Round 1 Round 2
Round 4Round 3
How does the algorithm loop through all the pairs of bubbles in a round?
Click for Answer
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1]Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
if (Bubble[test] > Bubble[test + 1])
{
}
for (test = 0; test <= lastTest; ++test)
{
}
int lastTest = 3;
The last test is the second to last element number in the array.
Round 1 Round 2
Round 4Round 3
How does the algorithm loop through all the rounds?
Click for Answer
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1]Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
if (Bubble[test] > Bubble[test + 1])
{
}
for (test = 0; test <= lastTest; ++test)
{
}
int lastTest = 3;
--lastTest;
}
while (lastTest > 0)
{
When the last test element number is at the top of the array, the algorithm has looked at all the possibility.
Round 1 Round 2
Round 4Round 3
Can the algorithm be made more efficient? Click for Answer
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1];Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
if (Bubble[test] > Bubble[test + 1])
{
}
for (test = 0; test <= lastTest; ++test)
{
}
int lastTest = 3;
--lastTest;
}
while ( lastTest > 0)
{
int DidItSwap = 1;
DidItSwap = 0;
DidItSwap = 1;
DidItSwap != 0 &&
If there is ever a round where no values change places, then the array is fully sorted.
Round 1 Round 2
Round 4Round 3
Be sure you understand this algorithm completely.
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1]Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
if (Bubble[test] > Bubble[test + 1])
{
}
for (test = 0; test <= lastTest; ++test)
{
}
int lastTest = 3;
--lastTest;
}
while (DidItSwap != 0 && lastTest > 0)
{
int DidItSwap = 1;
DidItSwap = 0;
DidItSwap = 1;
How would you change the algorithm to sort the bubbles from high to low?
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1]Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
if (Nums[test] > Nums[test + 1])
{
}
for (test = 0; test <= lastTest; ++test)
{
}
int lastTest = 4;
--lastTest;
}
while (DidItSwap != 0 && lastTest > 0)
{
int DidItSwap = 1;
DidItSwap = 0;
DidItSwap = 1;
SwapValue = Bubble[test];Bubble[test] = Bubble[test + 1]Bubble[test + 1] = SwapValue;
int Bubble[5];int SwapValue;int test;
if (Bubble[test] Bubble[test + 1])
{
}
for (test = 0; test <= lastTest; ++test)
{
}
int lastTest = 3;
--lastTest;
}
while (DidItSwap != 0 && lastTest > 0)
{
int DidItSwap = 1;
DidItSwap = 0;
DidItSwap = 1;
<
Click for Answer