Processing & Properties of Floor and Wall Tiles.pptx
Sorting Algorithms
1. Chameli Devi School Of Engineering
CSE Department
Presentation On
Presented By- Pranay Neema
Sorting Algorithms
2. 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
3. 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.
4. 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
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 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
7. 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.
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
• 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;
}
10. 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.
11. 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
12. 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;
}
}
13. 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…
15. 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.
17. 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 )
21. 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.
22. Conclusion
• Various use of sorting algorithm.
• Advancement in sorting algorithm day by day.
• Modification in previous algorithm.
• Each algorithm have it own importance.