+ All Categories
Home > Documents > Basic Sorting. What is sorting Arranging a LIST in a specific order –ascending –Descending.

Basic Sorting. What is sorting Arranging a LIST in a specific order –ascending –Descending.

Date post: 16-Dec-2015
Category:
Upload: isabel-atkinson
View: 228 times
Download: 1 times
Share this document with a friend
Popular Tags:
37
Basic Sorting
Transcript

Basic Sorting

What is sorting

• Arranging a LIST in a specific order

–ascending

–Descending

Recall

• So far we know that Lists are implemented using arrays.

• There are other ways to implement lists

– Dynamically

• Arrays

• Linked lists

Examples• Telephone directories

– In Last name order• Dictionaries

– In letter order• League Tables

– In points gained order• Bank Statements

– In date of transaction order

What sort of real world things are stored in lists?

Records and fields

Data often is organised as records of a number of fields or members. At the moment you would implement this using parallel arrays.

A record is like a row in a table. Each row consists of a number of columns or fields.

e.g. In a telephone book a record has the following fields.

Last Name, Initials, Address, Phone Number

Consider a library database

Records of books contain the following fields.

ISBN TITLE AUTHOR STOCK

This could be implemented using 4 parallel arrays.e.g.const int SIZE = 100;int ISBNS[SIZE];char TITLES[SIZE][80];char AUTHORS[SIZE][80];int STOCK[SIZE];

Sort Keys

If the parallel arrays were arranged in order of author name, then the field author name is a sort key.

If the data is sorted fist by one sort key and then in the instance of any ties by a second sort key, the first sort key is called the Major Key the second key is the Minor Key

Example the library recordsSuppose we have the following records stored in parallel arrays.

330

108

586

090

911

569

255

Childhood’s End

Janissaries

Hero’s Journey

Dantes Inferno

Animal Farm

I Robot

A Martian Odyddey

A.C.Clarke

J.Pournelle

S.E.Lanier

A.C.Clarke

G.Orwell

I.Asimov

S.G.Weiban

8

6

5

7

1

2

4

ISBN Array Title Array Author Array Stock Array

Example the library recordsSorted by Major key (Author) and minor key (Title).

330

108

586

090

911

569

255

Childhood’s End

Dantes Inferno

Animal Farm

I Robot

Janissaries

Hero’s Journey

A Martian Odyddey

8

6

5

7

1

2

4

ISBN ArrayTitle ArrayAuthor Array Stock Array

A.C.Clarke

A.C.Clarke

G.Orwell

I.Asimov

J.Pournelle

S.E.Lanier

S.G.Weiban

Why sort?

• To enable more efficient algorithms

– Binary search

• Data is more meaningful in order

• Data may have need a specific order e.g. Financial forecasts needs data in chronological order.

Categories of sort• Internal Sorts (for small data sets)

– Selection: Unsorted list scanned repeatedly for smallest key

• Simple selection

– Insertion: Each record taken in turn and inserted in correct position

• Simple insertion

– Exchange: Keys of two records are compared and swapped if one bigger than other.

• Bubble

• External Sorts (for large data sets): not covered

Selection Sort

Description1. find and put the smallest value exchange it

with first element. 2. Then find next smallest and exchange

with second element.3. Then find third smallest and exchange

with third element.4. and so on …

Selection Sort

Psudocode for a list of N items

FOR N-1 Passes DO

Find the smallest key in unsorted list

Swap the this key with the one at the top of the sorted area

END FOR

Selection Sorttop index smallest index

0 2swap 21, 9

1 1swap 13, 13

2 3swap 21, 15

3 4swap 21, 17

21 159 13 17

15 179 13 21

99 152121 13 17

15 21219 13 1717

2121 15159 13 17

0 1 2 3 4

Selection Sort// prototypes

void sort(double [ ], int nItems);void swap(double &, double &);

int main(void) {int index;double my_list[ ] = {21, 13, 9, 15, 17};

sort(my_list,5); // function call

cout<<"\nThe sorted array is: \n";for(index=0; index<5; index++)

cout<<'\t'<<my_list[index]<<endl;

return 0;}

Selection Sortvoid SelectionSort(double data[ ], int nItems) { int n, pass, sm_index;

double smallest;

for(pass=0; pass<nItems-1; pass++) {smallest=data[pass];sm_index=pass;for( n=pass+1; n<nItems; n++)

if (data[n]<smallest) {smallest=data[n];sm_index=n;

}if(sm_index != pass) //call swap when needed

swap(data[ sm_index ], data[ pass ]); }

}

Selection Sort

void swap(double & smaller, double & bigger){ double temp;

temp=bigger;bigger = smaller;smaller=temp;

}

Insertion SortDescription

This sort also uses the idea of a sorted area in the list.

Initially the first key is considered to be the sorted area.

Then the next key is compared with the first and in necessary exchanged.

Then the third key is considered relative to the first two keys and inserted.

Then the forth key is considered and so on.

Insertion SortPseudocode

For N-1 Passes DO

Make a copy of the item to be inserted

Find the insertion position

Make room by shuffling elements

Put copy of element in new position

END FOR

Insertion Sorttop index insertion pos

1 0

2 0

3 2

4 3

Sorted List

99 1513 21 17

15 179 13 21

9 1521 1313 17

15 219 13 1717

21 15159 13 17

0 1 2 3 4

void InsertionSort(int data[], int nItems){int pass;int i;int insertpoint;int nextInsert;

for (pass =1;pass<nItems;pass++){//make copy of next elementnextInsert = data[pass];//find insertion pointi = 0;while(data[i]< data[pass])

i++;insertpoint = i;//shuffle records down from insertion pointfor(i=pass-1; i >=insertpoint; i--)

data[i+1] = data[i];//put item in space made by shuffledata[insertpoint] = nextInsert;

}}

Bubble Sort

Keys of adjacent elements are compared and exchanged. Starting at one end of list then working element by element to other end.

The effect is that either the smallest element in the unsorted portion of list is moved (bubbled) to the lowest part of the unsorted list. Or the largest element is moved (bubbled) to the highest part of the unsorted list.

Bubble SortPseudocode

FOR N-1 Passes DO

Starting at beginning of list

FOR the number of items in the unsorted part of the list DO

IF the next two items are out of order THEN

exchange (swap) them

ENDIF

ENDFOR

ENDFOR

Bubble Sort (bubbles up largest)Put smaller first

Put smaller first

No change

Put smaller first

End of 1st Pass largest at end

21 252513 9 1717

2121 252513 9 17

99 252121 13 17

9 2513 21 17

21 1713 9 2525

Bubble Sort

Put smaller first

No change

Put smaller first

End of 2nd Pass

21 179 13 2525

17 21219 13 2525

2121 171313 9 2525

2121 17179 13 2525

Begin second pass

Bubble Sort

No change

No change

No change

No change

17 21219 13 2525

1717 21219 13 2525

1717 21219 13 2525

17 21219 13 2525

Begin third pass

Bubble Sort

No change

No change

No change

No change

17 21219 13 2525

1717 21219 1313 2525

1717 21219 13 2525

17 21219 13 2525

Begin forth pass

Bubble SortNote do not need to do fifth pass since the remaining unsorted part contains only one element

Forth and final pass also has no exchanges.Note that keeping track on the number of exchanges in a pass is a way of improving efficiency. We can stop the sort (using a return statement) if there are zero swaps in a pass.

A Bubble Sort Functionvoid BubbleSort(int data[], int nItems){

int pass, k;

for(pass=1; pass<=nItems ; pass++) { for(k=0; k < (nItems-pass); k++) {

if (data[k+1] < data[k]) { // > low to highswap(data[k+1],data[k]);

}}

}}

void swap(int & smaller, int & bigger){ int temp;

temp=bigger;bigger = smaller;smaller=temp;

}

Comparison of sortsWhen analysing sorts we need to get an estimate for the number of comparisons and the number of moves made.

This entails detailed scrutiny of loops and counting the number of if executions on average and the number of swaps or shuffles on average.

This does require a relatively experienced analyst. You can find derivations for these in most data structures books. All these sorts are of order O(N2). VERY SLOW!

But which to choose?

Sort Comparisons Moves Total

Selection 190 29 219

Insertion 95 114 209

Bubble 190 285 475

An empirical results of sorting lists of 20 items.

What you need to knowDifference in algorithms. I will not expect you to be able to remember the code for these sorts.

You must be able to trace the change in arrays step by step for each of these sort algorithms. That is given an initial array diagram, draw the subsequent states of the array, up to the point of it being sorted.

Meaning of terms: sort key, major key, minor key, field, record.

Array Review• Static data structure can store up to a fixed

maximum number of items.– Use a separate variable for each array to keep

track of the number of values actually stored in the array.

– E.g. int vals[1000]; int nvals = 0;

• Used to store a list of information of the same type.– Can be unordered– Can be ordered

Array Review

• Can be 1-, 2-, or multi- dimensional • Like any other variable they must be declared

before used• Element index starts at zero.• Can refer to out of bounds elements which can

crash your program.• Arrays initialised (filled) on declaration for very

small numbers, via keyboard for small number of items or via a data file for large numbers of items

Basic Operations (You should know)• Initialisation/building• Insertion• Deletion• Display List• Search List

– Linear– Binary (ordered list only)

• Sort List– Selection– Insertion– Bubble– And many others

Typical Applications of Arrays you should know

• Accumulate every element in list• Calculate average, standard deviation.

• Repeat calculations on every element.• Repeat calculations on adjacent elements.

• Calculate differences• Find max and min values in list• Filter list (extract a sub set of list)• Use as a lookup table

• Information systems

• Games

When to Use ordered Arrays• Use an ordered list when there will be few

insertions and deletions.• Searching is very fast.

• Use an ordered list if you need to generate reports for people to read• we find order meaningful

• Otherwise use an unordered list.• Insertion and deletion is very fast. Search is

slow.


Recommended