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

What's hot (20)

Hashing
HashingHashing
Hashing
Sri Prasanna
 
Chapter 2.2 data structures
Chapter 2.2 data structuresChapter 2.2 data structures
Chapter 2.2 data structures
sshhzap
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Moniruzzaman _
 
Maps
MapsMaps
Maps
Joyjit Choudhury
 
Why you should care about functional programming
Why you should care about functional programmingWhy you should care about functional programming
Why you should care about functional programming
Dhananjay Nene
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
arushi bhatnagar
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
Prof. Dr. K. Adisesha
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
Shahzad
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
C# p8
C# p8C# p8
C# p8
Renas Rekany
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Data structure
Data structureData structure
Data structure
Muhammad Farhan
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
Qundeel
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its Fundamentals
Hitesh Mohapatra
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
Nivegeetha
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
Dr Nisha Arora
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
ManishPrajapati78
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
Praveen M Jigajinni
 
Chapter 2.2 data structures
Chapter 2.2 data structuresChapter 2.2 data structures
Chapter 2.2 data structures
sshhzap
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Moniruzzaman _
 
Why you should care about functional programming
Why you should care about functional programmingWhy you should care about functional programming
Why you should care about functional programming
Dhananjay Nene
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
Shahzad
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
Qundeel
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its Fundamentals
Hitesh Mohapatra
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
ManishPrajapati78
 

Similar to Data structure presentation sorting (20)

my docoment
my docomentmy docoment
my docoment
NeeshanYonzan
 
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit LionovAlgorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Data structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptxData structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Chapter 2 Sorting and Searching .pptx.soft
Chapter 2 Sorting and Searching  .pptx.softChapter 2 Sorting and Searching  .pptx.soft
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.pptChapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
INDEX SORT
INDEX SORTINDEX SORT
INDEX SORT
Waqas Tariq
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
Arrays And Pointers in C programming language
Arrays And Pointers in C programming languageArrays And Pointers in C programming language
Arrays And Pointers in C programming language
Jasminelobo2
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
Praveen M Jigajinni
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
Preetiverma538521
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
WrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
Nielmagahod
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
Terry Yoast
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit LionovAlgorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Data structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptxData structure chapter 2 Time complexity of known algorithms.pptx
Data structure chapter 2 Time complexity of known algorithms.pptx
fikadumeuedu
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Chapter 2 Sorting and Searching .pptx.soft
Chapter 2 Sorting and Searching  .pptx.softChapter 2 Sorting and Searching  .pptx.soft
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.pptChapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Arrays And Pointers in C programming language
Arrays And Pointers in C programming languageArrays And Pointers in C programming language
Arrays And Pointers in C programming language
Jasminelobo2
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
WrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
Nielmagahod
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 

Recently uploaded (20)

DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITSDE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
Sridhar191373
 
UNIT-1-PPT-Introduction about Power System Operation and Control
UNIT-1-PPT-Introduction about Power System Operation and ControlUNIT-1-PPT-Introduction about Power System Operation and Control
UNIT-1-PPT-Introduction about Power System Operation and Control
Sridhar191373
 
microcontroller AHB Protocol presentation
microcontroller AHB Protocol presentationmicrocontroller AHB Protocol presentation
microcontroller AHB Protocol presentation
manohemanth1
 
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 / HIFLUX Co., Ltd.
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos12
 
22PCOAM16 Machine Learning Unit V Full notes & QB
22PCOAM16 Machine Learning Unit V Full notes & QB22PCOAM16 Machine Learning Unit V Full notes & QB
22PCOAM16 Machine Learning Unit V Full notes & QB
Guru Nanak Technical Institutions
 
ISO 5011 Air Filter Catalogues .pdf
ISO 5011 Air Filter Catalogues      .pdfISO 5011 Air Filter Catalogues      .pdf
ISO 5011 Air Filter Catalogues .pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Video Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptxVideo Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptx
HadiBadri1
 
HVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdfHVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Salesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for EveryoneSalesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for Everyone
ImtiazBinMohiuddin
 
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCHUNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
Sridhar191373
 
Advanced Concrete Technology- Properties of Admixtures
Advanced Concrete Technology- Properties of AdmixturesAdvanced Concrete Technology- Properties of Admixtures
Advanced Concrete Technology- Properties of Admixtures
Bharti Shinde
 
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITSDIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
Sridhar191373
 
Better Builder Magazine, Issue 53 / Spring 2025
Better Builder Magazine, Issue 53 / Spring 2025Better Builder Magazine, Issue 53 / Spring 2025
Better Builder Magazine, Issue 53 / Spring 2025
Better Builder Magazine
 
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdfISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
ISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdf
ISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdfISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdf
ISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
The Computer use in our modern lives .pptx
The Computer use in our modern lives .pptxThe Computer use in our modern lives .pptx
The Computer use in our modern lives .pptx
TamerHamed13
 
PPT on Grid resilience against Natural disasters.pptx
PPT on Grid resilience against Natural disasters.pptxPPT on Grid resilience against Natural disasters.pptx
PPT on Grid resilience against Natural disasters.pptx
manesumit66
 
All about the Snail Power Catalog Product 2025
All about the Snail Power Catalog  Product 2025All about the Snail Power Catalog  Product 2025
All about the Snail Power Catalog Product 2025
kstgroupvn
 
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITSDE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
Sridhar191373
 
UNIT-1-PPT-Introduction about Power System Operation and Control
UNIT-1-PPT-Introduction about Power System Operation and ControlUNIT-1-PPT-Introduction about Power System Operation and Control
UNIT-1-PPT-Introduction about Power System Operation and Control
Sridhar191373
 
microcontroller AHB Protocol presentation
microcontroller AHB Protocol presentationmicrocontroller AHB Protocol presentation
microcontroller AHB Protocol presentation
manohemanth1
 
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 / HIFLUX Co., Ltd.
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos12
 
Video Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptxVideo Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptx
HadiBadri1
 
Salesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for EveryoneSalesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for Everyone
ImtiazBinMohiuddin
 
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCHUNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
Sridhar191373
 
Advanced Concrete Technology- Properties of Admixtures
Advanced Concrete Technology- Properties of AdmixturesAdvanced Concrete Technology- Properties of Admixtures
Advanced Concrete Technology- Properties of Admixtures
Bharti Shinde
 
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITSDIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
Sridhar191373
 
Better Builder Magazine, Issue 53 / Spring 2025
Better Builder Magazine, Issue 53 / Spring 2025Better Builder Magazine, Issue 53 / Spring 2025
Better Builder Magazine, Issue 53 / Spring 2025
Better Builder Magazine
 
The Computer use in our modern lives .pptx
The Computer use in our modern lives .pptxThe Computer use in our modern lives .pptx
The Computer use in our modern lives .pptx
TamerHamed13
 
PPT on Grid resilience against Natural disasters.pptx
PPT on Grid resilience against Natural disasters.pptxPPT on Grid resilience against Natural disasters.pptx
PPT on Grid resilience against Natural disasters.pptx
manesumit66
 
All about the Snail Power Catalog Product 2025
All about the Snail Power Catalog  Product 2025All about the Snail Power Catalog  Product 2025
All about the Snail Power Catalog Product 2025
kstgroupvn
 

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; }