SlideShare a Scribd company logo
SORTING
BY:
Aashutosh Bhardwaj
Himank Jerolia
Pranjali
INTRODUCTION
 Sorting refers to arranging a set of data in a logical order.
 Arrangement of the data in ascending or descending order.
 Sorting is among most basic problem in algorithm design.
 Can be done on names, numbers and records
 Example: Telephone Directory ,Dictionary.
 Being unique, phone number can work as a key to locate any record in the list.
Sorting arranges data in a
sequence which makes
searching easier.
The two main criterias to judge which
algorithm is better than the other have
been:
1)Time taken to sort the given data.
2)Memory Space required to do so.
SORTING EFFICIENCY
DIFFERENT SORTING ALGORITHMS
 Bubble Sort
 Insertion Sort
 Selection Sort
 Quick Sort
 Merge Sort
 Heap Sort
Done by comparing two
adjacent elements.
Simplest technique.
For n elements, n-1 pass
required.
BUBBLE SORTING
Data structure presentation sorting
Data structure presentation sorting
Conceptually simplest algo.
Finds smallest no and swaps with
element at first position.
Repeatedly selects the next smallest
no and swaps it into right place hence
the name.
SELECTION SORT
Data structure presentation sorting
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of uns/* Function to print an array */
orted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: n");
printArray(arr, n);
return 0;
}
Efficient for smaller data.
Better than selection and bubble sort.
Space complexity less.
Stable sorting technique.
INSERTION SORT
Data structure presentation sorting
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}

More Related Content

PPTX
Data structure and algorithm All in One
PDF
Advance data structure & algorithm
DOCX
Functions reading passage
PDF
Recursion Lecture in C++
PPT
Sql
PDF
Recursion Lecture in Java
PPTX
Files,blocks and functions in R
Data structure and algorithm All in One
Advance data structure & algorithm
Functions reading passage
Recursion Lecture in C++
Sql
Recursion Lecture in Java
Files,blocks and functions in R

What's hot (20)

ZIP
Hashing
PPT
Chapter 2.2 data structures
PPTX
Arrays in Data Structure and Algorithm
PPSX
Dynamic memory allocation
PDF
Why you should care about functional programming
PPTX
arrays of structures
PDF
8 python data structure-1
PPT
Data Structure In C#
PPTX
Data structure , stack , queue
PDF
PPTX
Data structures and algorithms
PPT
Data structure
PPT
02 Arrays And Memory Mapping
PDF
Data Structure and its Fundamentals
PPTX
Adt of lists
PPTX
Recursion and Sorting Algorithms
PDF
3 Data Structure in R
PPT
Data Structure and Algorithms
PPTX
Chapter 17 Tuples
Hashing
Chapter 2.2 data structures
Arrays in Data Structure and Algorithm
Dynamic memory allocation
Why you should care about functional programming
arrays of structures
8 python data structure-1
Data Structure In C#
Data structure , stack , queue
Data structures and algorithms
Data structure
02 Arrays And Memory Mapping
Data Structure and its Fundamentals
Adt of lists
Recursion and Sorting Algorithms
3 Data Structure in R
Data Structure and Algorithms
Chapter 17 Tuples
Ad

Similar to Data structure presentation sorting (20)

PPTX
my docoment
PPTX
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
Data structure chapter 2 Time complexity of known algorithms.pptx
PPTX
searching in data structure.pptx
PPTX
Sequential & binary, linear search
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPT
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
PPT
Algorithms with-java-advanced-1.0
PDF
INDEX SORT
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
PDF
PPTX
Sorting pnk
PPTX
Arrays And Pointers in C programming language
PPT
PPT
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
PPT
chapter7 Sorting.ppt
PPT
chapter7.ppt
PPT
Sorting Seminar Presentation by Ashin Guha Majumder
PPT
Chapter 14
my docoment
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Searching and Sorting Algorithms in Data Structures
Data structure chapter 2 Time complexity of known algorithms.pptx
searching in data structure.pptx
Sequential & binary, linear search
Chapter 2 Sorting and Searching .pptx.soft
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Algorithms with-java-advanced-1.0
INDEX SORT
Chapter 3 - Data Structure and Algorithms.pptx
Sorting pnk
Arrays And Pointers in C programming language
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7 Sorting.ppt
chapter7.ppt
Sorting Seminar Presentation by Ashin Guha Majumder
Chapter 14
Ad

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
PPT on Performance Review to get promotions
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
Well-logging-methods_new................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Geodesy 1.pptx...............................................
PPTX
Construction Project Organization Group 2.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CYBER-CRIMES AND SECURITY A guide to understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CH1 Production IntroductoryConcepts.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT on Performance Review to get promotions
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
573137875-Attendance-Management-System-original
R24 SURVEYING LAB MANUAL for civil enggi
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
additive manufacturing of ss316l using mig welding
Well-logging-methods_new................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Geodesy 1.pptx...............................................
Construction Project Organization Group 2.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

Data structure presentation sorting

  • 2. INTRODUCTION  Sorting refers to arranging a set of data in a logical order.  Arrangement of the data in ascending or descending order.  Sorting is among most basic problem in algorithm design.  Can be done on names, numbers and records  Example: Telephone Directory ,Dictionary.  Being unique, phone number can work as a key to locate any record in the list.
  • 3. Sorting arranges data in a sequence which makes searching easier.
  • 4. The two main criterias to judge which algorithm is better than the other have been: 1)Time taken to sort the given data. 2)Memory Space required to do so. SORTING EFFICIENCY
  • 5. DIFFERENT SORTING ALGORITHMS  Bubble Sort  Insertion Sort  Selection Sort  Quick Sort  Merge Sort  Heap Sort
  • 6. Done by comparing two adjacent elements. Simplest technique. For n elements, n-1 pass required. BUBBLE SORTING
  • 9. Conceptually simplest algo. Finds smallest no and swaps with element at first position. Repeatedly selects the next smallest no and swaps it into right place hence the name. SELECTION SORT
  • 11. #include <stdio.h> void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } void selectionSort(int arr[], int n) { int i, j, min_idx; // One by one move boundary of uns/* Function to print an array */ orted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element swap(&arr[min_idx], &arr[i]); } } void printArray(int arr[], int size) { int i; for (i=0; i < size; i++) printf("%d ", arr[i]); printf("n"); } // Driver program to test above functions int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr)/sizeof(arr[0]); selectionSort(arr, n); printf("Sorted array: n"); printArray(arr, n); return 0; }
  • 12. Efficient for smaller data. Better than selection and bubble sort. Space complexity less. Stable sorting technique. INSERTION SORT
  • 14. void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i-1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; } } // A utility function to print an array of size n void printArray(int arr[], int n) { int i; for (i=0; i < n; i++) printf("%d ", arr[i]); printf("n"); } /* Driver program to test insertion sort */ int main() { int arr[] = {12, 11, 13, 5, 6}; int n = sizeof(arr)/sizeof(arr[0]); insertionSort(arr, n); printArray(arr, n); return 0; }