Docsity

Sorting Techniques: Bubble, Selection, Insertion, Quick, Merge, Heap Sort, Slides of Data Structures and Algorithms

Partial preview of the text

Download Sorting Techniques: Bubble, Selection, Insertion, Quick, Merge, Heap Sort and more Slides Data Structures and Algorithms in PDF only on Docsity!

Sorting

Sorting

  • (^) Sorting is the process of putting a list or a group of items in a

specific order

  • (^) Some common sorting criteria are alphabetical or numerical
  • (^) In telephone directory if the names of subscribers are not

arranged in alphabetic order it will be very difficult to find a

particular subscribers telephone number in a big city

  • (^) In a record there will be different fields and the records can be

arranged according to different fields

Sorting

  • (^) Some algorithms are very fast like Quick Sort
  • (^) Some algorithms need extra space for execution like the Merge

sort

  • (^) Some algorithms are very simple like bubble sort
  • (^) Some algorithms are good when the number of records to be

sorted is less than 100 records etc

  • (^) Merge Sort is both an external sorting technique as well as an

internal sorting technique

Sorting

We will study the following sorting techniques

1) Bubble Sort

2) Selection Sort

3) Insertion Sort

4) Quick Sort

5) Merge Sort

6) Heap Sort

(^36 85 45 14 10) indx = 0 large=x[0] 0 1 2 3 4 36 85 45 14 10

indx = 0 large=x[0] 0 1 2 3 4 36 85 45 14 10 x[indx] = x[i] x[i] = large

void selectionsort (int x[], int n)

int i, indx, j, large;

for (i = n-1; i>0; i--)

//Place the largest number of x[0] through x[i] into large and its

index into indx

large = x[0];

indx = 0;

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

{ if (x[j] > large )

{ large = x[j];

indx = j;

x[indx] = x[i];

x[i] = large;

The time complexity of selection sort is O (n

2