SlideShare a Scribd company logo
1 of 21
SORTING ALGORITHM GRAPHICAL 
METHOD 
Made by- 
Shantanu 
BCA-V sem. 
Guided by 
Mr. Sanjeev Kumar Singh
INTRODUCTION 
• Sorting of an array is the process of arranging the data present is an 
array in a particular manner either by ascending or descending order. 
• A sorting algorithm is an algorithm that puts elements of a list in a 
certain order. 
• The most used orders are numerical order and lexicographical order.
WHAT DOES SORTS DO? 
I am taking an example:- 
Take: [6,24,10,76,35,0,37] 
Make the above pattern into this:[0,6,10,24,35,37,76]
OBJECTIVE 
The main objective of my project is to graphically show sorting 
algorithm by arranging the records in ascending order. 
The language used in my project is .NET .
BASIC FLOW DIAGRAM 
USER 
SELECTION 
OF SORTING 
ALGORITHM 
SORTED ARRAY 
OUTPUT BY 
GRAPHICAL 
REPRESENTATION
SORTING METHODS 
The popular sorting methods are: 
• Bubble sort 
• Selection sort 
• Insertion sort 
• Merge sort 
• Quick sort
HOW FAST CAN WE SORT? 
• Selection Sort, Bubble Sort, Insertion Sort : 
O(n2) 
• Heap Sort, Merge sort : 
O(n log n) 
• Quicksort : 
• Average: O(n log n) 
• Best: O(n log n) or O(n)
BUBBLE SORT 
• Bubble Sort works by comparing each element of the list with the 
element next to it and swapping them if required. With each pass, the 
largest of the list is "bubbled" to the end of the list whereas the 
smaller values sink to the bottom.
ILLUSTRATION OF BUBBLE SORT 
Consider the following array of four items: 
Now I will sort this array using bubble sort: 
FIRST ITERATION : 
1st pass: 
90 45 70 15 
90 45 70 15 
45 90 70 15
2nd pass: 
3rd pass: 
SECOND ITERATION : 
1st pass: 
2nd pass: 
45 70 90 15 
45 70 15 90 
45 70 15 90 
45 70 15 90 
45 15 70 90
THIRD ITERATION : 
1st pass: 
45 15 70 90 
15 45 70 90 
As seen from the above illustration , that for four elements, we need 
three iterations . It means that if there are n elements then there will 
be n-1 iterations.
IMPLIMENTATION IN C 
void BubbleSort(int a[], int array_size) 
{ 
int i, j, temp; 
for (i = 0; i < (array_size - 1); ++i) 
{ 
for (j = 0; j < array_size - 1 - i; ++j ) 
{ 
if (a[j] > a[j+1]) 
{ 
temp = a[j+1]; 
a[j+1] = a[j]; 
a[j] = temp; 
} 
} 
} 
}
SELECTION SORT 
• The idea of Selection Sort is rather simple. It basically determines the 
minimum (or maximum) of the list and swaps it with the element at 
the index where its supposed to be. The process is repeated such that 
the nth minimum (or maximum) element is swapped with the 
element at the (n-1)th index of the list.
ILLUSTRATION OF SELECTION SORT 
Consider the following array of four items: 
Now I will sort this array using selection sort: 
1st pass: 
2nd pass: 
3rd pass: 
90 45 70 15 
15 90 45 70 
15 45 90 70 
15 45 70 90
IMPLIMENTATION IN C 
for(i=0;i<=10;i++) 
{ 
for(j=i+1;j<=10;j++) 
{ 
if(a[i]>a[j]) 
{ 
t=a[i]; 
a[i]=a[j]; 
a[j]=t; 
} 
} 
}
INSERTION SORT 
The Insertion Sort algorithm is a commonly used algorithm. In this type 
of sorting algorithm , the array is divided into two parts : the sorted 
and unsorted array. The very first element of the array is treated as 
sorted array and the rest of array is treated as the unsorted array . For 
sorting, first of all the first element of the unsorted array is inserted in 
the sorted array. After that the second element is inserted . The 
process continues till the last element of the unsorted array is inserted 
in the sorted array
ILLUSTRATION OF INSERTION SORT 
Consider the following array of four items: 
90 45 70 15 
90 
45 
70 
15 
Sorted Array 
Unsorted Array
After 1st iteration: 
45 
90 
70 
15 
After 2nd iteration: 
Sorted Array 
Unsorted Array 
45 
70 
90 
15 
Sorted Array 
Unsorted Array
After 3rd iteration: 
15 
45 
70 
90 
Sorted Array 
From the above illustration , we see that for an array of four elements , 
we need to process the array three times to get the sorted array. It 
means that if there are n elements then there will be n-1 iterations.
IMPLIMENTATION IN C 
void insertionSort(int a[], int array_size) 
{ 
int i, j, index; 
for (i = 1; i < array_size; ++i) 
{ 
index = a[i]; 
for (j = i; j > 0 && a[j-1] > index; j--) 
{ 
a[j] = a[j-1]; 
} 
a[j] = index; 
} 
}
THANK YOU

More Related Content

What's hot

Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
ryokollll
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
Rafay Farooq
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 

What's hot (20)

Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Sorting
SortingSorting
Sorting
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Sorting ppt
Sorting pptSorting ppt
Sorting ppt
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with Animation
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 

Viewers also liked

Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)
Arvind Devaraj
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
eShikshak
 
A history of science (volume 1)
A history of science (volume 1) A history of science (volume 1)
A history of science (volume 1)
Dipoceanov Esrever
 
Ancient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionAncient Ideas of Creation & Evolution
Ancient Ideas of Creation & Evolution
John Lynch
 

Viewers also liked (20)

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)
 
Parallel sorting algorithm
Parallel sorting algorithmParallel sorting algorithm
Parallel sorting algorithm
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++
 
Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithm
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision Algorithm
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Binary Search Algorithm
Binary Search Algorithm Binary Search Algorithm
Binary Search Algorithm
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
 
A history of science (volume 1)
A history of science (volume 1) A history of science (volume 1)
A history of science (volume 1)
 
Ancient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionAncient Ideas of Creation & Evolution
Ancient Ideas of Creation & Evolution
 

Similar to sorting algorithm graphical method

CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfSorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxBubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
_fahad_shaikh
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 

Similar to sorting algorithm graphical method (20)

Bin Sorting And Bubble Sort By Luisito G. Trinidad
Bin Sorting And Bubble Sort By Luisito G. TrinidadBin Sorting And Bubble Sort By Luisito G. Trinidad
Bin Sorting And Bubble Sort By Luisito G. Trinidad
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfSorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdf
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxBubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptx
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
 
Arrays
ArraysArrays
Arrays
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 

Recently uploaded

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

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
 
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.
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
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 ...
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
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
 

sorting algorithm graphical method

  • 1. SORTING ALGORITHM GRAPHICAL METHOD Made by- Shantanu BCA-V sem. Guided by Mr. Sanjeev Kumar Singh
  • 2. INTRODUCTION • Sorting of an array is the process of arranging the data present is an array in a particular manner either by ascending or descending order. • A sorting algorithm is an algorithm that puts elements of a list in a certain order. • The most used orders are numerical order and lexicographical order.
  • 3. WHAT DOES SORTS DO? I am taking an example:- Take: [6,24,10,76,35,0,37] Make the above pattern into this:[0,6,10,24,35,37,76]
  • 4. OBJECTIVE The main objective of my project is to graphically show sorting algorithm by arranging the records in ascending order. The language used in my project is .NET .
  • 5. BASIC FLOW DIAGRAM USER SELECTION OF SORTING ALGORITHM SORTED ARRAY OUTPUT BY GRAPHICAL REPRESENTATION
  • 6. SORTING METHODS The popular sorting methods are: • Bubble sort • Selection sort • Insertion sort • Merge sort • Quick sort
  • 7. HOW FAST CAN WE SORT? • Selection Sort, Bubble Sort, Insertion Sort : O(n2) • Heap Sort, Merge sort : O(n log n) • Quicksort : • Average: O(n log n) • Best: O(n log n) or O(n)
  • 8. BUBBLE SORT • Bubble Sort works by comparing each element of the list with the element next to it and swapping them if required. With each pass, the largest of the list is "bubbled" to the end of the list whereas the smaller values sink to the bottom.
  • 9. ILLUSTRATION OF BUBBLE SORT Consider the following array of four items: Now I will sort this array using bubble sort: FIRST ITERATION : 1st pass: 90 45 70 15 90 45 70 15 45 90 70 15
  • 10. 2nd pass: 3rd pass: SECOND ITERATION : 1st pass: 2nd pass: 45 70 90 15 45 70 15 90 45 70 15 90 45 70 15 90 45 15 70 90
  • 11. THIRD ITERATION : 1st pass: 45 15 70 90 15 45 70 90 As seen from the above illustration , that for four elements, we need three iterations . It means that if there are n elements then there will be n-1 iterations.
  • 12. IMPLIMENTATION IN C void BubbleSort(int a[], int array_size) { int i, j, temp; for (i = 0; i < (array_size - 1); ++i) { for (j = 0; j < array_size - 1 - i; ++j ) { if (a[j] > a[j+1]) { temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } } }
  • 13. SELECTION SORT • The idea of Selection Sort is rather simple. It basically determines the minimum (or maximum) of the list and swaps it with the element at the index where its supposed to be. The process is repeated such that the nth minimum (or maximum) element is swapped with the element at the (n-1)th index of the list.
  • 14. ILLUSTRATION OF SELECTION SORT Consider the following array of four items: Now I will sort this array using selection sort: 1st pass: 2nd pass: 3rd pass: 90 45 70 15 15 90 45 70 15 45 90 70 15 45 70 90
  • 15. IMPLIMENTATION IN C for(i=0;i<=10;i++) { for(j=i+1;j<=10;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } }
  • 16. INSERTION SORT The Insertion Sort algorithm is a commonly used algorithm. In this type of sorting algorithm , the array is divided into two parts : the sorted and unsorted array. The very first element of the array is treated as sorted array and the rest of array is treated as the unsorted array . For sorting, first of all the first element of the unsorted array is inserted in the sorted array. After that the second element is inserted . The process continues till the last element of the unsorted array is inserted in the sorted array
  • 17. ILLUSTRATION OF INSERTION SORT Consider the following array of four items: 90 45 70 15 90 45 70 15 Sorted Array Unsorted Array
  • 18. After 1st iteration: 45 90 70 15 After 2nd iteration: Sorted Array Unsorted Array 45 70 90 15 Sorted Array Unsorted Array
  • 19. After 3rd iteration: 15 45 70 90 Sorted Array From the above illustration , we see that for an array of four elements , we need to process the array three times to get the sorted array. It means that if there are n elements then there will be n-1 iterations.
  • 20. IMPLIMENTATION IN C void insertionSort(int a[], int array_size) { int i, j, index; for (i = 1; i < array_size; ++i) { index = a[i]; for (j = i; j > 0 && a[j-1] > index; j--) { a[j] = a[j-1]; } a[j] = index; } }