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

21
Internal Sorting

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

Page 1: 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.

Internal Sorting

Page 2: 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.

• 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.

Page 3: 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.

• 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.

Page 4: 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.

• 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.

Page 5: 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.

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.

Page 6: 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.

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.

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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; } } }}

Page 11: 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.

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

Page 12: 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.

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.

Page 13: 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.

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.

Page 14: 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.

#include <iostream.h>

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

Page 15: 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.

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

Page 16: 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.

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

Page 17: 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.

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;}}

Page 18: 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.

void main(){

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

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

Page 19: 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.

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.

Page 20: 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.

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.

Page 21: 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.

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;

}

}