sorting algorithms

24
Chameli Devi School Of Engineering CSE Department Presentation On Presented By- Pranay Neema Sorting Algorithms

Upload: pranay-neema

Post on 10-Jan-2017

78 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Sorting Algorithms

Chameli Devi School Of Engineering

CSE DepartmentPresentation On

Presented By- Pranay Neema

Sorting Algorithms

Page 2: Sorting Algorithms

Algorithm

• An algorithm is a procedure or formula for solving a problem. 

• The word derives from the name of the mathematician, Mohammed ibn-Musa al-Khwarizmi

Page 3: Sorting Algorithms

Algorithm Complexity• Most of the primary sorting algorithms run on different space

and time complexity.

• Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case).

• Space complexity is defined to be the amount of memory the computer needs to run a program.

Page 4: Sorting Algorithms

Complexity Conti..

• Complexity in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm.

• External Factors (not related to complexity):o Size of the input of the algorithmo Speed of the Computero Quality of the Compiler

Page 5: Sorting Algorithms

What is Sorting?Sorting: an operation that segregates items into groups according to specified criterion.

Example 1: A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 }

Example 2: B={S B F A D T U} B={A B D F S T U}

Page 6: Sorting Algorithms

Types of Sorting Algorithms

There are many, many different types of sorting algorithms, but the primary ones are:

Bubble Sort Selection SortInsertion Sort Merge Sort

Shell Sort Heap SortQuick Sort Radix Sort

Swap Sort

Page 7: Sorting Algorithms

What’s Different in Sorting Algorithm

• Time Complexity• Space Complexity• Internal Sorting : The data to be sorted is all stored in the

computer’s main memory.• External Sorting : Some of the data to be sorted might be

stored in some external, slower, device.• In place Sorting : The amount of extra space required to sort

the data is constant with the input size.

Page 8: Sorting Algorithms

Conti..• Stable Sorting : It sort preserves relative order of records

with equal keys.• Unstable Sorting : It doesn't preserves relative order.

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

Original array

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

Stably sorted

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

Original array

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

Unstably sorted

Page 9: Sorting Algorithms

Bubble Sort• Simple and uncomplicated • Largest element placer at last• Compare neighboring elements• Swap if out of order• Two nested loops• O(n2)

for (i=0; i<n-1; i++) { for (j=0; j<n-1-i; j++)

if (a[j+1] < a[j]) { /* compare neighbors */

tmp = a[j]; /* swap a[j] and a[j+1] */

a[j] = a[j+1]; a[j+1] = tmp;

}

Page 10: Sorting Algorithms

Selection Sort• The list is divided into two sublists, sorted and unsorted.

• Search through the list and find the smallest element.

• swap the smallest element with the first element.

• repeat starting at second element and find the second smallest element.

Page 11: Sorting Algorithms

23 78 45 8 32 56

8 78 45 23 32 56

8 23 45 78 32 56

8 23 32 78 45 56

8 23 32 45 78 56

8 23 32 45 56 78

Original List

After pass 1

After pass 2

After pass 3

After pass 4

After pass 5

Sorted Unsorted

Page 12: Sorting Algorithms

Selection Sort Algorithmpublic static void selectionSort(int[] list)

{ int min; int temp;

for(int i = 0; i < list.length - 1; i++) { min = i; for(int j = i + 1; j < list.length; j++)

if( list[j] < list[min] ) min = j; temp = list[i]; list[i] = list[min]; list[min] = temp; }}

Page 13: Sorting Algorithms

Insertion Sort• Another of the O(N^2) sorts• The first item is sorted• Compare the second item to the first• if smaller swap• Third item, compare to item next to it need to swap• after swap compare again• And so forth…

Page 14: Sorting Algorithms

Conti..

Page 15: Sorting Algorithms

Quick Sort• It is based on the principal of Divide-and-Conquer.• It works as follows:

1. First, it partitions an array into two parts, 2. Then, it sorts the parts independently, 3. Finally, it combines the sorted subsequences by

a simple concatenation.1. Divide: Partition the list.2. Recursion: Recursively sort the sublists separately.3. Conquer: Put the sorted sublists together.

Page 16: Sorting Algorithms

Quicksort Example13

8192

43 3165

5726

750

1381

9243 31

6557

2675

0

1343

31 5726 0 81 92 7565

13 4331 57260 81 9275

13 4331 57260 65 81 9275

Select pivot

partition

Recursive call Recursive call

Merge

Page 17: Sorting Algorithms

Merge Sort• It is based on divide-and-conquer sorting algorithms.• Requires an extra array memory.• It is a recursive algorithm

o Divides the list into halves, o Sort each halve separately, and o Then merge the sorted halves into one sorted array.o Both worst case and average cases are O (n * log2n )

Page 18: Sorting Algorithms

Mergesort - Example6 3 9 1 5

4 7 2

5

4 7 26 3 9 1

6 3 9 1 7 25

46 3 19 5 4 27

3 6 1 9 2 74

5

2

4 5 71 3 6 9

1 2 3 4 5

7 8 9

divide

dividedividedivide

dividedivide

divide

merge merge

merge

merge

merge merge

merge

Page 19: Sorting Algorithms

Radix Sort – Example•Non Comparison sorting algorithm.•Time Complexity O(n).

Page 20: Sorting Algorithms

Comparison

Page 21: Sorting Algorithms

Future Scope• An important key to algorithm design is to use sorting as abasic building block, because once a set of items is sorted, many other problems become easy.

• Sorting algorithm using Parallel processing.

• In future, we shall explore and support it with experimental results on data which could not only be numeric but also text, audio, video, etc.

Page 22: Sorting Algorithms

Conclusion• Various use of sorting algorithm.

• Advancement in sorting algorithm day by day.

• Modification in previous algorithm.

• Each algorithm have it own importance.

Page 23: Sorting Algorithms

THANKYOU….

Page 24: Sorting Algorithms