0% found this document useful (0 votes)
3 views6 pages

Algorithms Section8

The document describes two sorting algorithms: Merge Sort and Counting Sort. Merge Sort divides an array into subarrays, sorts them, and merges them back, while Counting Sort counts the frequency of each element to determine their sorted positions. The document also outlines the best, worst, and average case complexities for these algorithms.

Uploaded by

nh696831
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)
3 views6 pages

Algorithms Section8

The document describes two sorting algorithms: Merge Sort and Counting Sort. Merge Sort divides an array into subarrays, sorts them, and merges them back, while Counting Sort counts the frequency of each element to determine their sorted positions. The document also outlines the best, worst, and average case complexities for these algorithms.

Uploaded by

nh696831
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

Algorithms Section (8)

Section Content:
Merge sort & Count sort

Merge sort:
is defined as a sorting algorithm that works by dividing an array into smaller
subarrays, sorting each subarray, and then merging the sorted subarrays back
together to form the final sorted array.

EX:

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Algorithm:
Merge (L,R,A)
nL=length(L)
nR=length(R)
i=j=k=0
while(i<nL&& j<nR){
If(L[i]<=R[j]){
A[k]=L[i]
i++
}else{
A[k]=R[j]
j++
}
k++}
While(i<nL){
A[k]=L[i]
i++
k++
}
While(j<nR){
A[k]=L[j]
j++
k++
}
Merge Sort(A){
n=length(A)
if(n<2)
return n
mid=floor(n/2)
left=array of size (mid)
right=array of size (n-mid)
for(i=0 to (mid-1))
left [i]=A[i]
for(i=mid to (n-1))
right[i-mid]=A[i]
MergeSort(left)
MergeSort(right)
Merge(left,right,A)
}

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Counting sort:
The basic idea behind Counting Sort is to count the frequency of each distinct
element in the input array and use that information to place the elements in their
correct sorted positions.

EX:
Step 1: Find out the maximum element from the given array.

Step 2: Initialize a countArray[]of length max+1 with all values as 0.

Step 3: count of each element in the input array, then store the count
number at index which the same value of element in the countArray[].

Step 4: each element in the countArray, means how many number<= the
element’s index.

Step 5: start from last element in input Array “3”, go to the index 3 in
count Array “7”, subtract 1 from the element 7-1=6, put “6” in count
Array at index 3, go to index 6 at the output Array and put in this index
element “3” that you get from input Array. (output Array is the array you
create to put element in it after sort, it is with same size of input Array)

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Step 6: In input Array “0”, go to the index 0 in count Array “2”, subtract
1 from the element 2-1=1, put “1” in count Array at index 0, go to index
1 at the output Array and put in this index element “0” that you get from
input Array.

Step 7: In input Array “3”, go to the index 3 in count Array “6”, subtract
1 from the element 6-1=5, put “5” in count Array at index 3, go to index
5 at the output Array and put in this index element “3” that you get from
input Array.

Step 8: In input Array “2”, go to the index 2 in count Array “4”, subtract
1 from the element 4-1=3, put “3” in count Array at index 2, go to index
3 at the output Array and put in this index element “2” that you get from
input Array.

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Step 9: In input Array “0”, go to the index 0 in count Array “1”, subtract
1 from the element 1-1=0, put “0” in count Array at index 0, go to index
0 at the output Array and put in this index element “0” that you get from
input Array.

Step 10: In input Array “3”, go to the index 3 in count Array “5”,
subtract 1 from the element 5-1=4, put “3” in count Array at index 4, go
to index 4 at the output Array and put in this index element “3” that you
get from input Array.

Step 11: In input Array “5”, go to the index 5 in count Array “8”,
subtract 1 from the element 8-1=7, put “7” in count Array at index 5, go
to index 7 at the output Array and put in this index element “5” that you
get from input Array.

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Step 12: In input Array “2”, go to the index 2 in count Array “3”,
subtract 1 from the element 3-1=2, put “2” in count Array at index 2, go
to index 2 at the output Array and put in this index element “2” that you
get from input Array.

Best case: when the array has only 1 element  O(1)


Worst case: O(n log n)
Average case: O(n log n)

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed

You might also like