0% found this document useful (0 votes)
16 views

Implement The Following Sorting Algorithms

Uploaded by

Anver S R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Implement The Following Sorting Algorithms

Uploaded by

Anver S R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Implement the following sorting algorithms.

(i) Selection Sort.

(ii) Bubble sort

(iii) Insertion Sort

(iv) Merge sort

(v) Quick sort

(vi) Heap Sort.

Main()

Read the size of array? N

Read the array elements to a[0] …. A[n-1]

Read the choice ch? 1- selection

2- bubble

3- Insertion

4- Merge

5 – Quick

6 – Heap
45, 37, 28, 78, 50, 14, 87, 43

Pass 0: entire array is unsorted

We are finding the position of smallest element in the unsorted array, we exchanging with the
element in position 0.

14, 37, 28, 78, 50, 45, 87, 43.

Small_index = 0;

For (j = 1 to n-1)

If(a[j]<a[small_index])

Small_index = j;

Exchange a[0] and a[small_index].

Display the contents of array;

14, 37, 28, 78, 50, 45, 87, 43.

Pass1: unsorted array is from a[1] to a[7]

Find the smallest element in the unsorted part.

Exchange the smallest with a[1]

Small_index = 1;

For (j = 2 to n-1)

If(a[j]<a[small_index])

Small_index = j;

Exchange a[1] and a[small_index].

Display the contents of array;

14, 28, 37, 78, 50, 45, 87, 43.

Pass2:
Small_index = 2;

For (j = 3 to n-1)

If(a[j]<a[small_index])

Small_index = j;

Exchange a[2] and a[small_index].

Display the contents of array;

14, 28, 37, 78, 50, 45, 87, 43.

Pass6:

Selection_Sort(A, n)

For(i= 0 to n-2)

{ Small_index = i;

For (j = i+1 to n-1)

{If(a[j]<a[small_index])

Small_index = j;

Exchange a[i] and a[small_index].

Display the array contents.

For(k=0 to n-1)

Print a[k];

}
45, 37, 28, 78, 50, 14, 87, 43

Pass 0:

37, 45, 28, 78, 50, 14, 87, 43

37, 28, 45,….

37, 28, 45, 78,

50, 78

14, 78

78, 87

37, 28, 45, 50, 14, 78, 43, 87

We are finding the position of smallest element in the unsorted array, we exchanging with the
element in position 0.

37, 28, 45, 50, 14, 78, 43, 87

The index variable j has to vary from 0 to n-2;

0–1

1–2

2–3

n-2 – n-1

For (j = 0 to n-i-2)

If(a[j]>a[j+1])

Exchanging a[j] and a[j+1]

Display the contents of array;

14, 37, 28, 78, 50, 45, 87, 43.

Pass1: The index variable j has to vary from 0 to n-3;

0–1

1–2

2–3
n-3 – n-2

Small_index = 1;

For (j = 2 to n-1)

If(a[j]<a[small_index])

Small_index = j;

Exchange a[1] and a[small_index].

Display the contents of array;

14, 28, 37, 78, 50, 45, 87, 43.

Pass2:

Small_index = 2;

For (j = 3 to n-1)

If(a[j]<a[small_index])

Small_index = j;

Exchange a[2] and a[small_index].

Display the contents of array;

14, 28, 37, 78, 50, 45, 87, 43.

Pass6:
Bubble_sort(A, n)

For (i=0 to n-2)

For (j = 0 to n-i-2)

If(a[j]>a[j+1])

Exchange a[j] and a[j+1]

Display the array contents……

You might also like