0% found this document useful (0 votes)
62 views11 pages

CH3 - Simple Sorting

Here are the answers: 1. Bubble sort first iteration: 10 comparisons, 9 swaps. The array would be: 0264175398 2. Insertion sort first iteration: 9 comparisons, 1 swap (or 0 swaps). The array would be: 01753985264 By L. Mutanu

Uploaded by

Brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views11 pages

CH3 - Simple Sorting

Here are the answers: 1. Bubble sort first iteration: 10 comparisons, 9 swaps. The array would be: 0264175398 2. Insertion sort first iteration: 9 comparisons, 1 swap (or 0 swaps). The array would be: 01753985264 By L. Mutanu

Uploaded by

Brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

INTRODUCTION TO DATA

STRUCTURES
By L. Mutanu

CH3: SIMPLE SORTING

SORTING

Because sorting is so important and potentially so timeconsuming, it has been the subject of extensive research in
computer science, and as a result various sort algorithms have
been developed that range from very simple to very complex.

We look at two simple sorting algorithms for now i.e. the


bubble sort and the insertion sort. Later on we shall look as
some complex sort algorithms.

Sorting simply means arranging data. While you can easily


arrange five numbers at a glance the computer cannot glance
and thus needs a way or arranging data (algorithms).

By L. Mutanu

CH3: SIMPLE SORTING

BUBBLE SORT

The easiest way to sort data is the bubble sort.


If you have two numbers you can compare them and swap them to put the
smaller one before the larger one if you are sorting in ascending order.
But what if we have more than two numbers? Since a computer can only make a
decision between two numbers at a time you would need to compare the first
and swap then move to the next pair and swap.

View Animatio
n

You continue until you reach the end. Your array may not be fully sorted but the
largest number has been pushed to the end. This must be true because, as soon
as you encounter the largest, youll end up swapping it every time you compare
two numbers, until eventually it reaches the end of the array.
Then repeat the process again. This time because the largest is at the end you
only need to go through N-1 elements and the next time N-2 and so on until you
are left with one element. This shows that we will repeat the process N-1 times.

By L. Mutanu

CH3: SIMPLE SORTING

BUBBLE SORT ALGORITHM

The simplified algorithm would be: The actual bubble algorithm should appear as follows:
void bubbleSort()
Go through the array swapping
{
Repeat this process N-1 times
int out, in;
for(out=10-1; out>1; out--) // repeats process N-1 times
for(in=0; in<out; in++) //Go though array
if( v[in] > v[in+1] ) //out of order?
{
double temp = v[in]; //swap them
v[in] = v[in+1];
v[in+1] = temp;
]
}

By L. Mutanu

CH3: SIMPLE SORTING

THE COMPLETE PROGRAM


#include <iostream>
using namespace std;
class ArrayBub
{private:
int v[100]; //array doubles
public:
void insert() //put element into array
{v[0]= 77;
v[1]= 99;
v[2]= 44;
v[3]= 55;
v[4]= 22;
v[5]= 88;
v[6]= 11;
v[7]= 00;
v[8]= 66;
v[9]= 33;
}
void display() //displays array contents
{for(int j=0; j<10; j++) //for each element,
cout << v[j] << " "; //display it
cout << endl;
}

void bubbleSort()
{
int out, in;
for(out=10-1; out>1; out--) // repeats
process N-1 times
for(in=0; in<out; in++) //Go though array
if( v[in] > v[in+1] ) //out of order?
{
double temp = v[in]; //swap them
v[in] = v[in+1];
v[in+1] = temp;
}
}
}; //end class ArrayBub
int main()
{
ArrayBub arr; //create the array
arr.insert(); //insert 10 items
arr.display(); //display items
arr.bubbleSort(); //bubble sort them
arr.display(); //display them again
return 0;
} //end main()

By L. Mutanu
Exercise:
Can you modify the program to sort the names of employees

CH3: SIMPLE SORTING

EFFICIENCY OF BUBBLE SORT

The BubbleSort algorithm makes N-1 comparisons on the first pass, N2 on the second, and so on, down to one comparison on the last pass.
Thus in total the number of comparisons will be (N-1) + (N-2) + (N-3)
+ ... + 1 = N*(N-1)/2

If N is large then -1 will not make a difference so we can ignore it and


represent this as: N2/2

A comparison doesn't translate to swapping if the data is in order thus


there will be fewer swaps than comparisons. Thus we can say for a
random sample a swap is necessary about half the time. So there will
be about N2/4 swaps. (worst case--> a swap is necessary with every
comparison
or best
case -->don't
no swaps.)
in
Big O notation
constants
count as we saw earlier thus both
swaps and comparisons are
proportional to N2. We thus say the
bubble sort runs in O(N2) time. This is
slower than any algorithm we have

CH3: SIMPLE SORTING

INSERTION SORT ALGORITHM

Let right element be the current


and store it in a temp variable
Compare current with left, if left
is larger swap. Compare current
with next left. If next left is
larger swap.
Repeat the comparison until
you reach the last left. We now
know current to last left are
sorted.
Let next right element be the
current and store it in a temp
variable. Repeat the process

77

99

44

55

22

99

77

99

44

55

22

99

77

99

44

55

22

77

99

99

55

22

77

99

99

55

22

77

77

99

55

22

44

77

99

55

22

44

CH3: SIMPLE SORTING

INSERTION SORT

This algorithm starts out like the bubble sort. It compares the
two top elements and the smallest is kept at the top.
It then compares the second and third element and smallest is
kept at position two. Position two is then compared with position
1 and swapping takes place if the smallest is in position 1.
It still executes in O(N2) time, but its about twice as fast as the
bubble sort if the data is almost sorted.
HINT: Whenever you see nested loops such as those in the
bubble sort, you can suspect that an algorithm runs in O(N 2)
time. The outer loop executes N times, and the inner loop
executes N (or perhaps N divided by some constant) times for
each cycle of the outer loop. This means youre doing
something approximately N*N or N2 times.
View
animation

CH3: SIMPLE SORTING

Compare current with left,


if left is larger swap.
Compare current with next
left. If next left is larger
swap.

Repeat the comparison


until you reach the last left.
We now know current to
last left are sorted.

Let next right element be


the current and store it in a
temp variable. Repeat the
process

void insertionSort()
{
int in, out;
for(out=1; out<10; out++) //loops n-1 times (1 to 9)
{
double temp = v[out];
//store right element in temp
in = out;
//start shifts at temp
while(in>0 && v[in-1] >= temp) //do to last left and if out of
order,
{
v[in] = v[in-1];
//swap
- -in;
//go left one position
}
v[in] = temp;
//insert marked item when not out
// of order and before last left. this is the
correct
//position
}
}

By L. Mutanu

Exercise: Complete the program

CH3: SIMPLE SORTING

EFFICIENCY OF THE INSERTION


SORT

On the first pass, it compares a maximum of one item. On the


second pass, its a maximum of two items, and so on, up to a
maximum of N1 comparisons on the last pass. This is:
1 + 2 + 3 + ... + N-1 = N*(N-1)/2
Because constants are not important we say insertion sort runs
in O(N2) time.

While the time taken is the same as for the bubble sort (it
makes the same number of comparisons) it takes a shorter
time to swap if the data is almost sorted.

Both algorithms however work well for small amounts of data.


For huge amounts of data you need better sort algorithms.

By L. Mutanu

CH3: SIMPLE SORTING

Here is an array of ten integers:


5389170264
1. Draw this array after the FIRST iteration of the large
loop in a bubble sort (sorting from smallest to largest).
Comment on how many comparisons and how many
swaps were made during the first iteration.
2. Draw this array after the FIRST iteration of the large
loop in an insertion sort (sorting from smallest to
largest). This iteration has shifted at least one item in
the array! Comment on how many comparisons and
how many swaps were made during the first iteration.

You might also like