Download presentation
Presentation is loading. Please wait.
Published byJodie Parsons Modified over 8 years ago
2
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There are so many things in our real life that we need to search, like a particular record in database, roll numbers in merit list, a particular telephone number, any particular page in a book etc. Sorting arranges data in a sequence which makes searching easier.
3
Sorting Efficiency There are many techniques for sorting. Implementation of particular sorting technique depends upon situation. Sorting techniques mainly depends on two parameters. – First parameter is the execution time of program, which means time taken for execution of program. – Second is the space, which means space taken by the program.
4
Types of Sorting There are many types of Sorting techniques, differentiated by their efficiency and space requirements. – Insertion Sort – Selection Sort – Bubble Sort
5
Insertion Sort It is a simple comparison based Sorting algorithm, which sorts the array by shifting elements one by one. – It has one of the simplest implementation – It is efficient for smaller data sets, but very inefficient for larger lists. – Insertion Sort is adaptive, that means it reduces its total number of steps if given a partially sorted list, hence it increases its efficiency. – It is better than Selection Sort and Bubble Sort algorithms. – Its space complexity is less.
6
Insertion Sort Example
7
Insertion Sort Sort it using insertion sort…
8
Big O Notation Big O notation is used in to describe the performance or complexity of an algorithm. O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set. O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set. O(N 2 ) represents an algorithm whose performance is directly proportional to the square of the size of the input data set.
9
Complexity of Insertion Sort Worst Case Time Complexity : O(n 2 ) Best Case Time Complexity : O(n) Average Time Complexity : O(n 2 ) Space Complexity : O(1) Where n are number of items.
10
Algorithm of Insertion Sort Step 1 − If it is the first element, it is already sorted. return 1; Step 2 − Pick next element Step 3 − Com pare with all elements in the sorted sub-list Step 4 − Find appropriate position Step 5 − Insert the value Step 6 − Repeat until list is sorted
11
Implementation of Insertion Sort int a[6] = {5, 1, 11, 26, 4, 3}; int i, j, key; for(i=1; i<6; i++) { key = a[i]; j = i-1; while(j>=0 && key < a[j]) { a[j+1] = a[j]; j--; } a[j+1] = key;} for (int j=0; j<6; j++) cout<<a[j]<<endl;
12
Selection Sort Selection sorting is the most simplest comparison based sorting algorithm. This algorithm first finds the smallest element in the array and exchanges it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continues in this way until the entire array is sorted.
13
Selection Sort Example
14
Selection Sort Sort it using selection sort…
15
Complexity of Selection Sort Worst Case Time Complexity : O(n 2 ) Best Case Time Complexity : O(n 2 ) Average Time Complexity : O(n 2 ) Space Complexity : O(1) Where n is the number of elements
16
Algorithm of Selection Sort Step 1 − Set MIN to location 0 Step 2 − Search the minim um element in the list Step 3 − Swap with value at location MIN Step 4 − Increment MIN to point to next element Step 5 − Repeat until list is sorted
17
Implementation of Selection Sort #include using namespace std; main(){ int arr[5]; int mini,temp; cout<<"Enter 5 numbers: "<<endl; for(int i=0; i<5; i++){ cin>>arr[i];} cout<<"Orignal entered numbers: "<<endl; for(int j=0; j<5; j++){ cout<<arr[j]; cout<<endl; } for(int r1=0;r1<4;r1++){ mini=r1; for(int r2=r1+1; r2<5; r2++) if(arr[r2]<arr[mini]) mini=r2; if(mini !=r1){ temp=arr[r1]; arr[r1]=arr[mini]; arr[mini]=temp;}} cout<<"Array sorted by selection sort is: "<<endl; for(int q=0; q<5; q++){ cout<<arr[q]; cout<<endl;} getch();}
18
Bubble Sort Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order. Sorting is done is passes. It is called Bubble sort, because with each iteration the smaller element in the list bubbles up towards the first place, just like a water bubble rises up to the water surface.
19
Bubble Sort Example 51428
20
First Pass ( 5 1 4 2 8 ) => ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) => ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) => ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) => ( 1 4 2 5 8 ) Now, since these elements are already in order (8 > 5), algorithm does not swap them
21
Second Pass ( 1 4 2 5 8 ) => ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) => ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4 5 8 ) => ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) => ( 1 2 4 5 8 ) Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
22
Third Pass ( 1 2 4 5 8 ) => ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) => ( 1 2 4 5 8 ) Now the list is sorted
23
Bubble Sort 45673432310 Sort it using bubble sort…
24
Complexity of Bubble Sort The complexity of Bubble Sort is O(n 2 ). Space complexity for Bubble Sort is O(1), because only single additional memory space is required for temp variable Best-case Time Complexity is O(n), it is when the list is already sorted.
25
Algorithm of Bubble Sort begin BubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort
26
Implementation of Bubble Sort int a[6] = {7, 1, 9, 2, 4, 3}; int i, j, temp; for(i=0; i<6; i++){ for(j=0; j<6-i-1; j++) { if( a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } for (int j=0; j<6; j++) cout<<a[j]<<endl;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.