Docsity

Overview of Sorting Algorithms: Selection, Insertion, Bubble, Shell, and Linked List Sort, Slides of Computer Science

Partial preview of the text

Download Overview of Sorting Algorithms: Selection, Insertion, Bubble, Shell, and Linked List Sort and more Slides Computer Science in PDF only on Docsity!

Sorting

Sorting Functions

• Standard Functions

– Item is the data type of items being sorted.

– key() extracts the key from an item.

– exch() exchanges two items.

– less() returns true if first item is less than second.

– compexch() compares and exchanges two items, if

needed.

– The sorts are written generically and assume that

these functions are implemented.

Using #define to make sorts generic

The following #define statements implement the required functions when sorting an array of integers. #include #include template void exch(Item &A, Item &B) { Item t = A; A = B; B = t; } template void compexch(Item &A, Item &B) { if (B < A) exch(A, B); } template void sort(Item a[], int l, int r) { for (int i = l+1; i <= r; i++) for (int j = i; j > l; j--) compexch(a[j-1], a[j]); } int main(int argc, char argv[]) { int i, N = atoi(argv[1]), sw = atoi(argv[2]); int a = new int[N]; if (sw) for (i = 0; i < N; i++) a[i] = 1000(1.0rand()/RAND_MAX); else { N = 0; while (cin >> a[N]) N++; } sort(a, 0, N-1); for (i = 0; i < N; i++) cout << a[i] << " "; cout << endl; } }

Selection Sort

  • Selection sort works by finding the smallest element in the array, and move that element the the front (by exchanging), and then finding the next smallest element and exchanging, and so forth.

template

void selection(Item a[], int l, int r)

{ for (int i = l; i < r; i++)

{ int min = i;

for (int j = i+1; j <= r; j++)

if (a[j] < a[min]) min = j;

exch(a[i], a[min]);

Insertion Sort

• Insertion sort is similar to how people sort

playing cards. They pick up each card at a time,

and insert the card into the proper position,

making space for the new card between existing

cards, if necessary.

void sort(Item a[], int l, int r) { int i, j; for (i = l+1; i <= r; i++) for (j = i; j > l; j--) compexch(a[j-1], a[j]); }

Insertion Sort (improved)

  • This code is an improved version of insertion sort. First, it does a single

assignment rather an exchange in the inner loop. Second, it moves the

smallest key to the first position. Third, it simplifies the terminal condition

for the inner loop (which it can only do because it made sure that less ()

becomes true before running off the end of the array. (how?)

template
void insertion(Item a[], int l, int r)
{ int i;
for (i = r; i > l; i--) compexch(a[i-1], a[i]);
for (i = l+2; i <= r; i++)
{ int j = i; Item v = a[i];
while (v < a[j-1])
{ a[j] = a[j-1]; j--; }
a[j] = v;
  • These improvements yield half the running time for random inputs.
Nothing comes for free. The
improved efficiency in the critical
section of code is a trade-off with
the expense of ensuring that the
minimum element is in the first
position. Testing was required to
determine if the trade-off was worth
it. It was....

Bubble Sort

Bubble sort is normally the first sort learned by

students. Scan the file from left to right, n times.

Compare each element with its upper neighbor.

Exchange if necessary.

template

void bubble(Item a[], int l, int r)

{ for (int i = l; i < r; i++)

for (int j = r; j > i; j--)

compexch(a[j-1], a[j]);

Bubble Sort Demo

• Bubble Sort works like selection sort, but its

cost is explicit in this representation because it

uses exchanges (data movement) to move the

minimum element from right to left, one

position at a time.

Shellsort Demo

• Shellsort is clearly much faster than the

others, even (for example) for a file that is

initially in reverse order or for a file that is ten

times larger.

• Shellsort Demo

• Shellsort Demo - Reverse Order

• Shellsort Demo - 10 times larger

Special sorts: Linked Lists

  • Sorting of linked listed is straightforward with selection sort with

the following slight modication. Since it’s easier to insert at the

head of the list, scan the list of the largest item, insert it at the

head, then scan the list for the (2 nd) largest item, insert it at the

head, etc. The helper function, findmax() is used to find the node

that points to the maximum element in the list.

link listselection(link h) { node dummy(0); link head = &dummy, out = 0; head->next = h; while (head->next != 0) { link max = findmax(head), t = max->next; max->next = t->next; t->next = out; out = t; } return out; }