sorting (bubble,merge,selection sort)
DESCRIPTION
Sorting techniques (Bubble,Merge,Selection sort)TRANSCRIPT
Group no. 6
• Shoaib Manzoor 23
• Mureed Hussain 43
• Muhammad Sajjad 24
• Jahanzeb Shaukat 46
• Usman Nasir 12
SORTING
The process in which we arrange collection of items in ascending or descending order is called sorting.
What is sorting?
Types of sorting
There are many kinds of sorting but here we will discuss only three types:
• Bubble Sort
• Selection Sort
• Merge Sort
Bubble Sort
Shoaib Manzoor
Algorithm
• for I=1 to n• for j=1 to N-I
• if list[j] > list[j+1]
• swap(list[j], list[j+1])
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;
}}
}
}
Selection SortMureed Hussain
• 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:
• 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
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
MERGE SORT
Muhammad Sajjad & Usman Nasir
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)
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
Thank You