SlideShare a Scribd company logo
1 of 20
www.iies.in PH: +91 98869 20008 enquiry@iies.in
Topic: Sorting in c programming
www.iies.in PH: +91 98869 20008 enquiry@iies.in
Why Sorting :
Sorting is one of the important category of algorithms in computer science. And a lot of
research has gone into this category. Sorting can significantly reduce the complexity of
problem and is often used for database algorithms and searches.
What is Sorting :
Sorting is an algorithm that arranges the data elements in a specified order, here the order
refers to ascending or descending.
www.iies.in PH: +91 98869 20008 enquiry@iies.in
C provides the following categories of sorting techniques:
1)Bubble Sort.
2)Insertion Sort.
3)Selection Sort.
4)Quick Sort.
5)Merge Sort.
www.iies.in PH: +91 98869 20008 enquiry@iies.in
Bubble sort:
bubble sort is a simple algorithm. It works by iterating the input array from the first element to the last, comparing
each pair of elements and swapping them if needed. Bubble sort continues its iteration until no swaps are needed.
The algorithm gets it name from the way smaller elements bubble up to the top of the list.
Now let us take an example :
Suppose we have an array which is to be sorted like this , a[ ] ={7,9,5,18,2}
Before sorting:
7 9 5 18 2
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
After sorting the array it should look like this
now this is our unsorted array
2 5 7 9 18
0 1 2 3 4
7 9 5 18 2
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
now in the 1ST PASS we compare the adjacent elements and do swapping if they are not in sorted form to get
them in sorted form and traverse to the next adjacent elements now for traversing what I can see is we compare the
adjacent elements and if it is already in sorted form then we move ahead with next adjacent elements without
swapping. What if they are not in sorted order we just simply swap the elements to place them in there right place.
To sort an n-size array we perform n-1 passes. Now let us see how actually we perform bubble sort and what
actually a pass means and what will happen at the end of each pass.
PASS:1
first we compare with index 0 and index 1.
7 9 5 18 2
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
since the elements are already in the sorted form we move ahead without swapping.
Now we compare with the index 1 and 2 since 5<9 we swap them and go ahead with next adjacent elements.
7 9 5 18 2
0 1 2 3 4
7 5 9 18 2
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
Now we are comparing the index 2 and 3 since 18 > 9 we are not swapping them and moving ahead with
next adjacent elements.
Now we are comparing with index 3 and index 4 since 2< 18 we are swapping their positions.
7 5 9 18 2
0 1 2 3 4
7 5 9 2 18
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
So now we completed PASS:1 and the complete array comparison and and sorted the highest element in the
array to its correct position
after PASS:1 array looks like.
7 5 9 2 18
0 1 2 3 4
Unsorted array Sorted array
www.iies.in PH: +91 98869 20008 enquiry@iies.in
PASS:2
Now we are comparing with index 0 and index1 since 5<7 we swap the elements and move ahead with the
next adjacent comparing.
Now we are comparing with index 1 and index 2 since they are in there right positions we are moving ahead
without swapping .
7 5 9 2 18
0 1 2 3 4
5 7 9 2 18
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
Now we are comparing with the index 2 and 3 since 2<9 we swap them and move ahead.
We can see that we have completed PASS:2 and the result is that 9 has reached its correct position in the
array. We have seen below that we have sorted two elements in two passes.
The output of second pass is :
5 7 9 2 18
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
This the output of second pass we have sorted two elements in two passes:
5 7 2 9 18
0 1 2 3 4
Unsorted array
Sorted array
www.iies.in PH: +91 98869 20008 enquiry@iies.in
PASS:3
now we are comparing the indices 0 and 1 since they are already in sorted positions we are not going to swap
them and proceed with next adjacent comparisons.
5 7 2 9 18
0 1 2 3 4
5 7 2 9 18
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
Now we are comparing with indices 1 and 2 since 2 < 7 we are going to swap them and not going to proceed
with next adjacent comparison because and 9 and 18 has already been sorted.
we have also finished the pass:3, The output of pass-3 is:
5 2 7 9 18
0 1 2 3 4
5 2 7 9 18
0 1 2 3 4
Unsorted array Sorted array
www.iies.in PH: +91 98869 20008 enquiry@iies.in
PASS:4
In pass 4 we have only one comparison because rest of the elements are already sorted.
We are comparing indices 0 and 1 since 2 < 5 we are swapping them and our array is fully sorted.
5 2 7 9 18
0 1 2 3 4
2 5 7 9 18
0 1 2 3 4
www.iies.in PH: +91 98869 20008 enquiry@iies.in
The output of PASS:4 is
This is our final output of bubble sort technique.
2 5 7 9 18
0 1 2 3 4
Fully sorted array
www.iies.in PH: +91 98869 20008 enquiry@iies.in
In pass:1 we did n-1 comparisons.
N=5 n-1= 5-1 = 4 comparisons
In pass:2 we did n-2comparisons.
N=5 n-1= 5-2 = 3 comparisons
In pass:3 we did n-3 comparisons.
N=5 n-1= 5-3 = 2 comparisons
In pass:4 we did n-4 comparisons.
N=5 n-1= 5-4 = 1 comparison.
www.iies.in PH: +91 98869 20008 enquiry@iies.in
PROGRAM FOR BUBBLE SORT ALGORITHM USING C PROGRAMMING LANGUAGE.
#include<stdio.h>
// for displaying the output
void DisPlay(int A[], int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
printf("%d ", A[i]);
}
printf("n");
}
www.iies.in PH: +91 98869 20008 enquiry@iies.in
// for bubblesort
void bubble_SorT(int A[], int SIZE){
int temp;
int Sorted = 0;
for (int i = 0; i < SIZE-1; i++) // For number of pass
{
// printf("Working on pass number %dn", i+1);
for (int j = 0; j <SIZE-1-i ; j++) // For comparison in each pass
{
if(A[j]>A[j+1]){
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
}
www.iies.in PH: +91 98869 20008 enquiry@iies.in
int main(){
int A[] = {11, 2, 5, 78,8,75,56,65,100,722,7,1};
int SIZE = sizeof(A)/sizeof(int);
DisPlay(A, SIZE); // array before sorting
bubble_SorT(A, SIZE); // to sort the array
DisPlay(A, SIZE); // array After sorting
return 0;
}

More Related Content

What's hot

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Circular linked list
Circular linked listCircular linked list
Circular linked listchauhankapil
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked ListNinad Mankar
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and LexemeA. S. M. Shafi
 

What's hot (20)

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Leftist heap
Leftist heapLeftist heap
Leftist heap
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Python set
Python setPython set
Python set
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Linked lists
Linked listsLinked lists
Linked lists
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Hash tables
Hash tablesHash tables
Hash tables
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
CS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna UniversityCS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna University
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
single linked list
single linked listsingle linked list
single linked list
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Red black tree
Red black treeRed black tree
Red black tree
 

Similar to Sorting Techniques in C programming

Quick Sort in data structure.pptx
Quick Sort in data structure.pptxQuick Sort in data structure.pptx
Quick Sort in data structure.pptxujjwalmatoliya
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityMotaleb Hossen Manik
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
Insertion sort presentation.pptx
Insertion sort presentation.pptxInsertion sort presentation.pptx
Insertion sort presentation.pptxSofiMusic
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)Neil Soliven
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...BhumikaBiyani1
 

Similar to Sorting Techniques in C programming (20)

Quick Sort in data structure.pptx
Quick Sort in data structure.pptxQuick Sort in data structure.pptx
Quick Sort in data structure.pptx
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Insertion sort presentation.pptx
Insertion sort presentation.pptxInsertion sort presentation.pptx
Insertion sort presentation.pptx
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Dynamic programming
Dynamic programming Dynamic programming
Dynamic programming
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 

Recently uploaded

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Sorting Techniques in C programming

  • 1. www.iies.in PH: +91 98869 20008 enquiry@iies.in Topic: Sorting in c programming
  • 2. www.iies.in PH: +91 98869 20008 enquiry@iies.in Why Sorting : Sorting is one of the important category of algorithms in computer science. And a lot of research has gone into this category. Sorting can significantly reduce the complexity of problem and is often used for database algorithms and searches. What is Sorting : Sorting is an algorithm that arranges the data elements in a specified order, here the order refers to ascending or descending.
  • 3. www.iies.in PH: +91 98869 20008 enquiry@iies.in C provides the following categories of sorting techniques: 1)Bubble Sort. 2)Insertion Sort. 3)Selection Sort. 4)Quick Sort. 5)Merge Sort.
  • 4. www.iies.in PH: +91 98869 20008 enquiry@iies.in Bubble sort: bubble sort is a simple algorithm. It works by iterating the input array from the first element to the last, comparing each pair of elements and swapping them if needed. Bubble sort continues its iteration until no swaps are needed. The algorithm gets it name from the way smaller elements bubble up to the top of the list. Now let us take an example : Suppose we have an array which is to be sorted like this , a[ ] ={7,9,5,18,2} Before sorting: 7 9 5 18 2 0 1 2 3 4
  • 5. www.iies.in PH: +91 98869 20008 enquiry@iies.in After sorting the array it should look like this now this is our unsorted array 2 5 7 9 18 0 1 2 3 4 7 9 5 18 2 0 1 2 3 4
  • 6. www.iies.in PH: +91 98869 20008 enquiry@iies.in now in the 1ST PASS we compare the adjacent elements and do swapping if they are not in sorted form to get them in sorted form and traverse to the next adjacent elements now for traversing what I can see is we compare the adjacent elements and if it is already in sorted form then we move ahead with next adjacent elements without swapping. What if they are not in sorted order we just simply swap the elements to place them in there right place. To sort an n-size array we perform n-1 passes. Now let us see how actually we perform bubble sort and what actually a pass means and what will happen at the end of each pass. PASS:1 first we compare with index 0 and index 1. 7 9 5 18 2 0 1 2 3 4
  • 7. www.iies.in PH: +91 98869 20008 enquiry@iies.in since the elements are already in the sorted form we move ahead without swapping. Now we compare with the index 1 and 2 since 5<9 we swap them and go ahead with next adjacent elements. 7 9 5 18 2 0 1 2 3 4 7 5 9 18 2 0 1 2 3 4
  • 8. www.iies.in PH: +91 98869 20008 enquiry@iies.in Now we are comparing the index 2 and 3 since 18 > 9 we are not swapping them and moving ahead with next adjacent elements. Now we are comparing with index 3 and index 4 since 2< 18 we are swapping their positions. 7 5 9 18 2 0 1 2 3 4 7 5 9 2 18 0 1 2 3 4
  • 9. www.iies.in PH: +91 98869 20008 enquiry@iies.in So now we completed PASS:1 and the complete array comparison and and sorted the highest element in the array to its correct position after PASS:1 array looks like. 7 5 9 2 18 0 1 2 3 4 Unsorted array Sorted array
  • 10. www.iies.in PH: +91 98869 20008 enquiry@iies.in PASS:2 Now we are comparing with index 0 and index1 since 5<7 we swap the elements and move ahead with the next adjacent comparing. Now we are comparing with index 1 and index 2 since they are in there right positions we are moving ahead without swapping . 7 5 9 2 18 0 1 2 3 4 5 7 9 2 18 0 1 2 3 4
  • 11. www.iies.in PH: +91 98869 20008 enquiry@iies.in Now we are comparing with the index 2 and 3 since 2<9 we swap them and move ahead. We can see that we have completed PASS:2 and the result is that 9 has reached its correct position in the array. We have seen below that we have sorted two elements in two passes. The output of second pass is : 5 7 9 2 18 0 1 2 3 4
  • 12. www.iies.in PH: +91 98869 20008 enquiry@iies.in This the output of second pass we have sorted two elements in two passes: 5 7 2 9 18 0 1 2 3 4 Unsorted array Sorted array
  • 13. www.iies.in PH: +91 98869 20008 enquiry@iies.in PASS:3 now we are comparing the indices 0 and 1 since they are already in sorted positions we are not going to swap them and proceed with next adjacent comparisons. 5 7 2 9 18 0 1 2 3 4 5 7 2 9 18 0 1 2 3 4
  • 14. www.iies.in PH: +91 98869 20008 enquiry@iies.in Now we are comparing with indices 1 and 2 since 2 < 7 we are going to swap them and not going to proceed with next adjacent comparison because and 9 and 18 has already been sorted. we have also finished the pass:3, The output of pass-3 is: 5 2 7 9 18 0 1 2 3 4 5 2 7 9 18 0 1 2 3 4 Unsorted array Sorted array
  • 15. www.iies.in PH: +91 98869 20008 enquiry@iies.in PASS:4 In pass 4 we have only one comparison because rest of the elements are already sorted. We are comparing indices 0 and 1 since 2 < 5 we are swapping them and our array is fully sorted. 5 2 7 9 18 0 1 2 3 4 2 5 7 9 18 0 1 2 3 4
  • 16. www.iies.in PH: +91 98869 20008 enquiry@iies.in The output of PASS:4 is This is our final output of bubble sort technique. 2 5 7 9 18 0 1 2 3 4 Fully sorted array
  • 17. www.iies.in PH: +91 98869 20008 enquiry@iies.in In pass:1 we did n-1 comparisons. N=5 n-1= 5-1 = 4 comparisons In pass:2 we did n-2comparisons. N=5 n-1= 5-2 = 3 comparisons In pass:3 we did n-3 comparisons. N=5 n-1= 5-3 = 2 comparisons In pass:4 we did n-4 comparisons. N=5 n-1= 5-4 = 1 comparison.
  • 18. www.iies.in PH: +91 98869 20008 enquiry@iies.in PROGRAM FOR BUBBLE SORT ALGORITHM USING C PROGRAMMING LANGUAGE. #include<stdio.h> // for displaying the output void DisPlay(int A[], int SIZE) { for (int i = 0; i < SIZE; i++) { printf("%d ", A[i]); } printf("n"); }
  • 19. www.iies.in PH: +91 98869 20008 enquiry@iies.in // for bubblesort void bubble_SorT(int A[], int SIZE){ int temp; int Sorted = 0; for (int i = 0; i < SIZE-1; i++) // For number of pass { // printf("Working on pass number %dn", i+1); for (int j = 0; j <SIZE-1-i ; j++) // For comparison in each pass { if(A[j]>A[j+1]){ temp = A[j]; A[j] = A[j+1]; A[j+1] = temp; } } } }
  • 20. www.iies.in PH: +91 98869 20008 enquiry@iies.in int main(){ int A[] = {11, 2, 5, 78,8,75,56,65,100,722,7,1}; int SIZE = sizeof(A)/sizeof(int); DisPlay(A, SIZE); // array before sorting bubble_SorT(A, SIZE); // to sort the array DisPlay(A, SIZE); // array After sorting return 0; }