0% found this document useful (0 votes)
8 views20 pages

Presentation 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

Presentation 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

By Subhradip Das

KALYANI GOVERNMENT ENGINEERING COLLEGE

MAKAUT

DSA
(Sorting Techniques)
 NAME : Subhradip Das
 ROLL : 10201621078(Branch Change)
 STERAM : Electronics and Communication Engineering
 SEMESTER : 3rd
 YEAR : 2nd
Content

 Introduction to Sorting
 Types of Sorting
 Selection Sort
 Insertion Sort
 Bubble Sort
 Quick Sort
 Merge Sort
SORTING
• Sorting is a process in which records are arranged in
ascending or descending order
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6

5 12 35 42 77 101
TYPES OF SORTING

• Selection sort • Quick sort


• Insertion sort
• Heap sort
• Bubble sort
• Merge sort • Shell sort
Selection Sort

• Selection sort is a sorting algorithm which works as follows:


• Find the minimum value in the list
• Swap it with the value in the first position
• Repeat the steps above for remainder of the list (starting at
the second position)
26 33 43 100 46 88 52 17 53 77
17 | 33 43 100 46 88 52 26 53 77
17 26 | 43 100 46 88 52 33 53 77
17 26 33 | 100 46 88 52 43 53 77
17 26 33 43 | 46 88 52 100 53 77 Example: Selection Sort
17 26 33 43 46 | 88 52 100 53 77
17 26 33 43 46 52 | 88 100 53 77
17 26 33 43 46 52 53 | 100 88 77
17 26 33 43 46 52 53 77 | 88 100
17 26 33 43 46 52 53 77 88 | 100
Selection Sort Command
void selectionSort(int numbers[], int array_size) {
int i, j, T, min, count;
for (i = 0; i < array_size; i++) {
min = i;
for (j = i + 1; j < array_size; j++) {
if (numbers[j] < numbers[min]) {
min = j; }
}
T = numbers[min];
numbers[min] = numbers[i];
numbers[i] = T;
}
}
Insertion Sort
• In insertion sort, each successive element in the array to
be sorted is inserted into its proper place with respect to
the other, already sorted elements.
• We divide our array in a sorted and an unsorted array
• Initially the sorted portion contains only one element: the
first element in the array.
• We take the second element in the array, and put it into
its correct place
36 36 24 10 6 6
24 24 36 24 10 10
10 10 10 36 24 12 Example: Selection Sort
6 6 6 6 36 24
12 12 12 12 12 36
Insertion Sort Algorithm
void insertionSort(int array[], int length)
{
int i, j, value;
for(i = 1; i < length; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[ j ] > value; j--)
{
a[j + 1] = a[ j ];
}
a[j + 1] = value;
}
}
Bubble Sort
1 2 3 4 5 6
• Bubble sort is similar to 42 35 12 77 5 101
selection sort in the sense
1 2 3 4 5 6
that it repeatedly finds the
largest/smallest value in the 35 12 42 5 77 101

unprocessed portion of the 1 2 3 4 5 6


array and puts it back. 12 35 5 42 77 101
• However, finding the largest
1 2 3 4 5 6
value is not done by selection
12 5 35 42 77 101
this time.
• We "bubble" up the largest 1 2 3 4 5 6
value instead. 5 12 35 42 77 101
Bubble Sort Algorithm

void bubbleSort (int S[ ], int length) {


bool isSorted = false;
while(!isSorted)
{
isSorted = true;
for(int i = 0; i<length; i++)
{
if(S[i] > S[i+1])
{
int temp = S[i];
S[i] = S[i+1];
S[i+1] = temp;
isSorted = false;
}
}
length--;
}
}
Merge Sort

• A divide-and-conquer algorithm:
• Divide the unsorted array into 2 halves until
the sub-arrays only contain one element
• Merge the sub-problem solutions together:
• Compare the sub-array’s first elements
• Remove the smallest element and put it
into the result array
• Continue the process until all elements
have been put into the result array
MergeSort Algorithm

Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves.

Merge(Passed two arrays)


Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output array)
Quick Sort

Quick sort is a divide and conquer algorithm.


Quick sort first divides a large list into two smaller sub-lists: the
low elements and the high elements. Quick sort can then
recursively sort the sub-lists.

The steps are:


1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements with values less than
the pivot come before the pivot, while all elements with
values greater than the pivot come after it (equal values can
go either way). After this partitioning, the pivot is in its final
position. This is called the partition operation.
3. Recursively apply the above steps to the sub-list of elements
with smaller values and separately the sub-list of elements
with greater values.
Quick Sort
void quickSort(int a[], int first, int last);
int pivot(int a[], int first, int last);
void swap(int& a, int& b);
Void main()
{
int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };
int N = sizeof(test)/sizeof(int);
quickSort(test, 0, N-1);
}
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
void quickSort( int a[], int first, int last) {
int pivotElement;
if(first < last) {
pivotElement = pivot(a, first, last);
quickSort(a, first, pivotElement-1);
quickSort(a, pivotElement+1, last); }
}
int pivot(int a[], int first, int last){
int p = first;
int pivotElement = a[first];
for(int i = first+1 ; i <= last ; i++){
if(a[i] <= pivotElement){
p++;
swap(a[i], a[p]);
}
}
swap(a[p], a[first]);
return p;
int pivot(int a[], int first, int last){
int p = first;
int pivotElement = a[first];
for(int i = first+1 ; i <= last ; i++){
if(a[i] <= pivotElement){
p++;
swap(a[i], a[p]);
}
}
swap(a[p], a[first]);
return p;
}
REFERENCES

http://www.csanimated.com/animation.php?t=Quicksort
http://www.hakansozer.com/category/c/
http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/
http://www.sorting-algorithms.com/shell-sort

You might also like