Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and Data Structures

Similar presentations


Presentation on theme: "Algorithms and Data Structures"— Presentation transcript:

1 Algorithms and Data Structures
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Sorting Course No.: Fall 2014

2 Definition It is a process for arranging a collection of items in an order. Sorting can arrange both numeric and alphabetic data in increasing or decreasing order. There are dozens of algorithms, the choice of which depends on factors such as the number of items relative to working memory, knowledge of the initial order of the items, the cost of comparing keys vs. the cost of moving items.

3 Complexity of Sorting The complexity function of sorting is a notation of different data comparison done while sorting the data items of an array. The sorting complexity is represented using Big O notation. It is defined as O(f(n)), where f(n) is a function of no. of data item sorted in an array. The complexity enables you to compare the run time required for each sorting technique.

4 Bubble Sort It is one of the oldest sorts known.
The bubble sort got its name because of the way the biggest elements "bubble" to the top. It based on the property of a sorted list that any two adjacent elements are in sorted order.

5 In a typical iteration of bubble sort each adjacent pair of elements is compared, starting with the first two elements, then the second and the third elements, and all the way to the final two elements. Each time two elements are compared, if they are already in sorted order, nothing is done to them and the next pair of elements is compared. In the case where the two elements are not in sorted order, the two elements are swapped, putting them in order.

6 Example Consider a set of data: 5 9 2 8 4 6 3.
Bubble sort first compares the first two elements, the 5 and the 9. Because they are already in sorted order, nothing happens. The next pair of numbers, the 9 and the 2 are compared. Because they are not in sorted order, they are swapped and the data becomes:

7 To better understand the "bubbling" nature of the sort, watch how the largest number, 9, "bubbles" to the top in the first iteration of the sort. First iteration (5 9) > compare 5 and 9, no swap 5 (9 2) > compare 9 and 2, swap 5 2 (9 8) > compare 9 and 8, swap 5 2 8 (9 4) > compare 9 and 4, swap (9 6) 3 --> compare 9 and 6, swap (9 3) --> compare 9 and 3, swap > first iteration complete Notice that in the example above, the largest element, the 9, got swapped all the way into its correct position at the end of the list. This happens because in each comparison, the larger element is always pushed towards its place at the end of the list.

8 Second iteration In the second iteration, the second-largest element will be bubbled up to its correct place in the same manner: (5 2) > compare 5 and 2, swap 2 (5 8) > compare 5 and 8, no swap 2 5 (8 4) > compare 8 and 4, swap 2 5 4 (8 6) > compare 8 and 6, swap (8 3) 9 --> compare 8 and 3, swap > no need to compare last two because the last element is known to be the largest.

9 Third iteration (2 5) >compare 2 and 5 no swap, 2 (5 4) >compare 5 and 4 swap, 2 4 (5 6) >compare 5 and 6 no swap, 2 4 5 (6 3) >compare 6 and 3 swap, >no need to compare the last three elements Fourth iteration (2 4) > compare 2 and 4, no swap 2 (4 5) > compare 4 and 5, no swap 2 4 (5 3) > compare 5 and 3, swap > no need to compare the last four elements. Fifth iteration (2 4) > compare 2 and 4, no swap 2 (4 3) > compare 4 and 3, swap > compare 4 and 5, no swap > no need to compare the last five elements.

10 // Bubble Sort #include<iostream.h> B_Sort(int x[],int n) { int temp; for(int i = n-1 ; i > 0 ; i--) for(int j = 0 ; j <= i-1 ; j++) if(x[j] > x[j+1]){ temp=x[j]; x[j]=x[j+1]; x[j+1]=temp; } print(int x[],int n) for(int i = 0 ; i < n ; i++) cout<<x[i]<<" "; cout<<endl; main() { int x[7]={9,0,1,4,2,3,7}; print(x,7); B_Sort(x,7); }

11 Time Estimate The worst case is the 1st loop is executed n-1 times.
The second loop is executed also n-1 times. (n -1)(n-1)= n2 -2n+1 Which is approximately O(n2 -2n+1)=O(n2)

12 Selection Method Selection Method : it select the largest item in the list and Swap it with the item in the last position in the list. Example:

13 Selection Sort: main() { int x[7] = {12,6,9,10,8,21,13 } ; print(x,7);
#include<iostream.h> Selection(int x[],int n) { int index; int large; for(int i = n-1 ; i > 0 ; i--) large = x[0]; index = 0; for(int j = 1 ; j <= i ; j++) { if(x[j] > large) large = x[j]; index = j; } x[index] = x[i]; x[i] = large; print(int x[],int n) for(int i = 0 ; i < n ; i++) cout<<x[i]<<" "; cout<<endl; main() { int x[7] = {12,6,9,10,8,21,13 } ; print(x,7); Selection (x,7); print(x,7);}

14 Time Estimate Assuming there are n elements in the array, we must call “max-key” function through n - 1 entries. For each entry, we may need to examine and shift up to n - 1 other entries. This resulting in a O(n2) algorithm.

15 Insertion Sort One of the simplest methods to sort an array is an insertion sort, If the first few objects are already sorted, an unsorted object can be inserted in the sorted set in proper place. Its try to reduce—the number of key comparisons. Insertion sort sorts the list by moving each element to its proper place. An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct sequence.

16 Example Consider the following list The length of the list is 8.
That is, list[0]...list[3] is sorted Because list[4] < list[3], we need to move the element list[4] to its proper location. To move list[4] into list[2], first we copy list[4] into temp, a temporary memory space Next, we copy list[3] into list[4], and then list[2] into list[3] Next we copy temp into list[2]. We repeat This process on the resulting list by moving the first element of the unsorted list into the sorted list in the proper place.

17 Example..Cont.

18 Insertion Sort #include<iostream.h> Insertion(int x[],int n) {
int index; int temp; for (int f = 1; f <= n-1; f++) if (x[f] < x[f-1]) temp = x[f]; index = f; while (index > 0 && x[index - 1] > temp){ x[index] = x[index - 1]; // copy index--; } x[index] = temp; //move print(int x[],int n) for(int i = 0 ; i < n ;i++) cout<<x[i]<<" "; cout<<endl; main() { int x[7] = {9,0,1,4,2,3,7}; print(x,7); Insertion(x,7); }

19 Time Estimate Assuming there are n elements in the array, we must index through n - 1 entries. For each entry, we may need to examine and shift up to n - 1 other entries. This resulting in a O(n2) algorithm.

20 ShellSort Algorithm Introduced in 1959 by D. E. Shell
In Shellsort, the elements of the list are viewed as sublists at a particular distance. Each sublist is sorted Example: suppose that you have a list of 15 elements. First we view the list as 7 sublists, that is, we sort the elements at a distance of 7.

21 Example

22 ShellSort Procedure Determine numbers h1…….ht of ways of dividing array data into sub arrays For (n=ht;t>1;t--) Divide data into n subarrays For(i=1;i<=n;i++) Sort subarray datai Sort array data

23 Quick Sort One of the most popular sorting algorithms is quicksort.
The quicksort algorithm works by partitioning the array to be sorted, then recursively sorting each partition. In Partition , one of the array elements is selected as a bound or pivot value. Values smaller than the bound value are placed to the left of the bound (lowerSublist), while larger or equal values are placed to the right (upperSublist ). The two subarrays can be sorted separately There are several ways to determine the bound . However, the bound is chosen so that, it is hoped, the lowerSublist and upperSublist are of nearly equal size.

24 Example 5,9,2 , 8, 4,6,3 QuickSort recursively sorts the two subarrays, the result: 2,3,4,5,6,8,9 pivot 8 9 5,2,4,6,3 pivot

25 QuickSort Procedure QuickSort ( array[], n) If n>1 Choose bound; while there are elements in the array If element <bound Put it in lowerSublist else Put it in upperSublist QuickSort (lowerSublist ); QuickSort (upperSublist );


Download ppt "Algorithms and Data Structures"

Similar presentations


Ads by Google