KEMBAR78
Sorting Algorithms | PPTX
Chameli Devi School Of Engineering
CSE Department
Presentation On
Presented By- Pranay Neema
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
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.
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 algorithm
o Speed of the Computer
o Quality of the Compiler
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}
Types of Sorting Algorithms
There are many, many different types of sorting algorithms,
but the primary ones are:
Bubble Sort Selection Sort
Insertion Sort Merge Sort
Shell Sort Heap Sort
Quick Sort Radix Sort
Swap Sort
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.
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
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;
}
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.
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
Selection Sort Algorithm
public 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;
}
}
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…
Conti..
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.
Quicksort Example
13
81
92
43 31
65
57
26
75
0
13
81
92
43 31
65
57
26
75
0
13
43
31 57
26 0 81 92 7565
13 4331 57260 81 9275
13 4331 57260 65 81 9275
Select pivot
partition
Recursive call Recursive call
Merge
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 )
Mergesort - Example
6 3 9 1 5 4 7 2
5 4 7 26 3 9 1
6 3 9 1 7 2
5 4
6 3 19 5 4 27
3 6 1 9 2 7
4 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
Radix Sort – Example
•Non Comparison sorting algorithm.
•Time Complexity O(n).
Comparison
Future Scope
• An important key to algorithm design is to use sorting as a
basic 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.
Conclusion
• Various use of sorting algorithm.
• Advancement in sorting algorithm day by day.
• Modification in previous algorithm.
• Each algorithm have it own importance.
THANKYOU….
Sorting Algorithms

Sorting Algorithms

  • 1.
    Chameli Devi SchoolOf Engineering CSE Department Presentation On Presented By- Pranay Neema Sorting Algorithms
  • 2.
    Algorithm • An algorithmis a procedure or formula for solving a problem. • The word derives from the name of the mathematician, Mohammed ibn-Musa al-Khwarizmi
  • 3.
    Algorithm Complexity • Mostof 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.
  • 4.
    Complexity Conti.. • Complexityin 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 algorithm o Speed of the Computer o Quality of the Compiler
  • 5.
    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}
  • 6.
    Types of SortingAlgorithms There are many, many different types of sorting algorithms, but the primary ones are: Bubble Sort Selection Sort Insertion Sort Merge Sort Shell Sort Heap Sort Quick Sort Radix Sort Swap Sort
  • 7.
    What’s Different inSorting 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.
  • 8.
    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
  • 9.
    Bubble Sort • Simpleand 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; }
  • 10.
    Selection Sort • Thelist 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.
  • 11.
    23 78 458 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
  • 12.
    Selection Sort Algorithm publicstatic 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; } }
  • 13.
    Insertion Sort • Anotherof 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…
  • 14.
  • 15.
    Quick Sort • Itis 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.
  • 16.
    Quicksort Example 13 81 92 43 31 65 57 26 75 0 13 81 92 4331 65 57 26 75 0 13 43 31 57 26 0 81 92 7565 13 4331 57260 81 9275 13 4331 57260 65 81 9275 Select pivot partition Recursive call Recursive call Merge
  • 17.
    Merge Sort • Itis 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 )
  • 18.
    Mergesort - Example 63 9 1 5 4 7 2 5 4 7 26 3 9 1 6 3 9 1 7 2 5 4 6 3 19 5 4 27 3 6 1 9 2 7 4 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
  • 19.
    Radix Sort –Example •Non Comparison sorting algorithm. •Time Complexity O(n).
  • 20.
  • 21.
    Future Scope • Animportant key to algorithm design is to use sorting as a basic 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.
  • 22.
    Conclusion • Various useof sorting algorithm. • Advancement in sorting algorithm day by day. • Modification in previous algorithm. • Each algorithm have it own importance.
  • 23.