Sorting (Bubble,Merge,Selection sort)

18

description

Sorting techniques (Bubble,Merge,Selection sort)

Transcript of Sorting (Bubble,Merge,Selection sort)

Page 1: Sorting (Bubble,Merge,Selection sort)
Page 2: Sorting (Bubble,Merge,Selection sort)
Page 3: Sorting (Bubble,Merge,Selection sort)

Group no. 6

• Shoaib Manzoor 23

• Mureed Hussain 43

• Muhammad Sajjad 24

• Jahanzeb Shaukat 46

• Usman Nasir 12

Page 4: Sorting (Bubble,Merge,Selection sort)

SORTING

Page 5: Sorting (Bubble,Merge,Selection sort)

The process in which we arrange collection of items in ascending or descending order is called sorting.

What is sorting?

Page 6: Sorting (Bubble,Merge,Selection sort)

Types of sorting

There are many kinds of sorting but here we will discuss only three types:

• Bubble Sort

• Selection Sort

• Merge Sort

Page 7: Sorting (Bubble,Merge,Selection sort)

Bubble Sort

Shoaib Manzoor

Page 8: Sorting (Bubble,Merge,Selection sort)

Algorithm

• for I=1 to n• for j=1 to N-I

• if list[j] > list[j+1]

• swap(list[j], list[j+1])

Page 9: Sorting (Bubble,Merge,Selection sort)

Source Code (C++)

Void main(int list[10]){

for(int i = 0 ; i < 10 ; i++){

for (int j = 0 ; j < i ; j++){

if (list[j] > list [j + 1]){

m = list[j];list[j] = list[j+ 1]list[j + 1] = m;

}}

}

}

Page 10: Sorting (Bubble,Merge,Selection sort)

Selection SortMureed Hussain

Page 11: Sorting (Bubble,Merge,Selection sort)

• The selection sort is a combination of searching and sorting.

• In selection sort, sorting is done after selecting a particular smallest or largest element from an array and shifted it to a particular location in an array.

• During each pass, the unsorted element with the smallest (or largest) value is moved to its proper position in the array.

• Let's look at our same table of elements using a selection sort for ascending order:

Page 12: Sorting (Bubble,Merge,Selection sort)

• The number of times the sort passes through the

• In the selection sort, the inner loop finds the next smallest (or largest) value and the outer loop places that value into its proper location.

• Lets watch a visualizer which will give you a basic idea of a selection sort.

• Please co-operate.

Array 40 50 60 10 30 20Array 40 50 60 10 30 20Array 40 50 60 10 30 20Array 40 50 60 10 30 20

1-pass 10 50 60 40 30 20

2-pass 10 20 60 40 30 50

3-pass 10 20 30 40 60 50

4-pass 10 20 30 40 60 50

5-pass 10 20 30 40 50 60

Page 13: Sorting (Bubble,Merge,Selection sort)

For (i=1 to n)m = ifor j = i+1 to n

if list[j] < list[m]m=j

if m != jswap(list[i] , list[m])

Algorithm

Page 14: Sorting (Bubble,Merge,Selection sort)

MERGE SORT

Muhammad Sajjad & Usman Nasir

Page 15: Sorting (Bubble,Merge,Selection sort)

AlgorithmFunction merge_sort(list)

if length of list < 2return list

left = empty listright = empty list

m = length of list / 2for i = 1 to m

append list i to leftfor j = m + 1 to length of list

append list j to rightreturn merge (merge_sort(left));

merge _sort (right)

Page 16: Sorting (Bubble,Merge,Selection sort)

function merge ( left , right )i = 1j = 1

result = empty listwhile i <= length of left

and j <= length of rightif left i < right j

append left i to resulti = i +1

elseappend right j to resultj = j + 1

for k = i to length of leftappend left k to result

for k = j to length of rightappend right k to result

return result

Page 17: Sorting (Bubble,Merge,Selection sort)
Page 18: Sorting (Bubble,Merge,Selection sort)

Thank You