Internal Sorting. Sorting is one of the most frequently performed computing tasks. We might sort the...

Preview:

Citation preview

Internal Sorting

• Sorting is one of the most frequently performed computing tasks. We might sort the records in a database so that we can search the collection efficiently.

• Because sorting is so important, naturally it has been studied intensively and many algorithms have been devised. Some of these algorithms are straightforward adaptations of schemes we use in everyday life.

• The collection of sorting algorithms presented will illustrate that divide-and-conquer is a powerful approach to solving a problem, and that there are multiple ways to do the dividing.

• Merge sort divides a list in half. • Quick sort divides a list into big values and

small values. • And Radix Sort divides the problem by

working on one digit of the key at a time.

Sorting Terminology and Notation

• Given a set of records r1, r2, ..., rn with key values k1, k2, ..., kn, the Sorting Problem is to arrange the records into any order s such that records rs1 , rs2 , ..., rsn have keys obeying the property ks1≤ks2 ≤:::≤ ksn. In other words, the sorting problem is to arrange a set of records so that the values of their key fields are in non-decreasing order.

Sorting Algorithms

• Bubble Sort Bubble Sort consists of a simple double for

loop. The first iteration of the inner for loop moves through the record array from left to right, comparing adjacent keys. If the lower-indexed key’s value is greater than its higher-indexed neighbour, then the two values are swapped.

#include <iostream.h>#include <conio.h>#define MAX 10 class bubsort{ int arr[MAX],n; public: void getdata(); void showdata(); void sortLogic();};

void bubsort :: getdata(){ cout<<"How many elements you require : "; cin>>n; for(int i=0;i<n;i++) cin>>arr[i];}

void bubsort :: showdata(){ cout<<"\n--Display--\n"; for(int i=0;i<n;i++) cout<<arr[i]<<" ";}

void bubsort :: sortLogic(){ int temp; for(int i=0;i<n-1;i++){ for(int j=0;j<n-i-1;j++){ if(arr[j] > arr[j+1]){ temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } }}

void main(){ cout<<"\n*****Bubble Sort*****\n"; bubsort obj; obj.getdata(); obj.sortLogic(); obj.showdata(); }

Insertion Sort

• Imagine that you have a stack of phone bills from the past two years and that you wish to organize them by date. A fairly natural way to do this might be to look at the first two bills and put them in order. Then take the third bill and put it into the right order with respect to the first two, and so on. As you take each bill, you would add it to the sorted pile that you have already made.

Insertion Sort

• This naturally intuitive process is the inspiration for our first sorting algorithm, called Insertion Sort. Insertion Sort iterates through a list of records. Each record is inserted in turn at the correct position within a sorted list composed of those records already processed.

#include <iostream.h>

#define MAX 10class inssort{ int arr[MAX],n; public: void getdata(); void showdata(); void sortLogic();};

void inssort :: getdata(){ cout<<"How many elements you require : "; cin>>n; for(int i=0;i<n;i++) cin>>arr[i];}

void inssort :: showdata(){ cout<<"\n--Display--\n"; for(int i=0;i<n;i++) cout<<arr[i]<<" ";}

void inssort :: sortLogic(){for(int i=1;i<n;i++){int temp=arr[i];for(int j=i;j>0&&arr[j-1]>temp;j--){arr[j]=arr[j-1];}arr[j]=temp;}}

void main(){

cout<<"\n*****Insertion Sort*****\n"; inssort obj;

obj.getdata(); obj.sortLogic(); obj.showdata(); }

Selection Sort

• Consider again the problem of sorting a pile of phone bills for the past year. Another intuitive approach might be to look through the pile until you find the bill for January, and pull that out. Then look through the remaining pile until you find the bill for February, and add that behind January. Proceed through the ever-shrinking pile of bills to select the next one in order until you are done. This is the inspiration for our sort, called Selection Sort.

Selection Sort

• The ith pass of Selection Sort “selects” the ith smallest key in the array, placing that record into position i. In other words, Selection Sort first finds the smallest key in an unsorted list, then the second smallest, and so on. Its unique feature is that there are few record swaps. To find the next smallest key value requires searching through the entire unsorted portion of the array, but only one swap is required to put the record in place.

void inssort :: sortLogic(){ int temp,min; for(int i=0;i<n;i++)

{ min=i; for(int j=i+1;j<n;j++)

{if(arr[min] > arr[j]){

min=j; } } temp = arr[min]; arr[min] = arr[i]; arr[i] = temp;

}

}

Recommended