SlideShare a Scribd company logo
1 of 29
Sorting
And Its
Types
SORTING
Sorting refers to operations of
arranging a set of data in a given
order.
10 20 30 40 50 60
30 10 60 20 50 40 Unsorted List
Sorted List
BASIC TYPES :
Internal Sorting:
If all the data to be sorted can be
adjusted in main memory then it is
called as Internal Sorting.
External Sorting:
If data to be stored is large and aquires
external memory then the type is called
as External Sorting.
METHODS OF SORTING:
Bubble Sort
Selection Sort
Insertion Sort
BUBBLE SORT
ALGORITHM
Bubble Sort:
Algorithm of bubble sort includes two steps
repeated until the list is sorted.
 Compare adjacent elements, if the
element on right side is smaller then
swap their positions.
 Compare first element, second element
and so on on completion of Pass 1 the
largest element is at last position.
Pass 1:
50 10 30 20 40
50 10 30 20 40
10 50 30 20 40
10 30 50 20 40
10 30 20 50 40
10 30 20 40 50
0,1
1,2
2,3
3,4
Number Of Passes = Max – 1 = 5 – 1 = 4
Pass 2:
10 30 20 40 50
10 30 20 40 50
10 30 20 40 50
10 30 20 40 50
0,1
1,2
2,3
Pass 3:
10 30 20 40 50
10 30 20 40 50
10 30 20 40 50
0,1
1,2
Pass 4:
10 30 20 40 50 0,1
10 30 20 40 50
10 30 20 40 50 Sorted list
To sort numbers in ascending order using bubble sort
technique
import java.util.*;
public class bubbleSort{
public static void main(String a[]) throws Exception
{
int i,j,n;
int a[] = {50,10,30,20,40};
System.out.println("Values Before the sort:n");
for(i = 0; i < a.length; i++)
System.out.print( a[i]+" ");
System.out.println();
n=a.length;
for(i = 1; i < n; i++)
{
for(j = 0; j < (n-i); j++)
{
if(a[j] > a[j+1]){
t = a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
System.out.print("Values after the sort:n");
for(i = 0; i <a.length; i++)
System.out.print(a[i]+" ");
} }
SELECTION SORT
ALGORITHM
Selection Sort:
Here in selection sort the algorithm depends on the
zeroth element majorly.
 The Zeroth element is compared with the first
element and if the element at right is found smaller
then their positions are swapped or exchanged.
 The same procedure is carried with all elements of
the list resulting into a fully sorted list.
23 15 29 11 1
23 15 29 11 1
Pass
1:
Number Of Passes = Max – 1 = 5 – 1 = 4
15 23 29 11 1
15 23 29 11 1
11 23 29 15 1
1 23 29 15 11
0,1
0,2
0,3
0,4
1 23 29 15 11
Pass 2:
1 29 23 15 11
1 15 23 29 11
1 11 23 29 15
1,2
1,3
1,4
1 11 23 29 15
Pass 3:
1 11 23 29 15
1 11 15 29 23
2,3
2,4
Pass 4:
1 11 15 29 23
1 11 15 23 29
1 11 15 23 29 Sorted List
3,4
To sort numbers in ascending order using selection
sort technique
import java.util.*;
public class selectionSort{
public static void main(String a[]) throws Exception
{
int i,j;
int a[] = {23,15,29,11,1};
System.out.println("Values Before the sort:n");
for(i = 0; i < a.length; i++)
System.out.print( a[i]+" ");
System.out.println();
n=a.length;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] > a[j]){
t = a[j];
a[j]=a[i];
a[i]=t;
}
}
}
System.out.print("Values after the sort:n");
for(i = 0; i <a.length; i++)
System.out.print(a[i]+" ");
}}
INSERTION SORT
Mathematical applications : in the search for
greater value, or the smallest value. In
many other applications.
This method is effective when dealing
with small numbers .
ALGORITHM
Insertion Sort
In insertion sort the elements are compared
and inserted to respective index place.
 It starts with comparision of 1st and 0th
element in pass 1.
 In pass 2 the second element is compared
with the 1st and 0th element.
 Doing so with all the elements in the list
appropriate element is inserted by shifting
elements on right.
public insertionSort( int [] a r r )
{
f or ( i n t i = 1; i < arr.Length; ++i)
{
i n t temp = a r r [ i ] ;
i n t pos = i ;
}
arr[pos] = temp;
}
}
Select
while (arr[pos-1].CompareTo(temp) > 0 &&pos > 0)
{
Comparing
arr[pos] = arr[pos-1];
pos--;
Shift
Insert
75 57 25 19 4
Pass 1:
75 57 25 19 4
Number Of Passes = Max – 1 = 5 – 1 = 4
57 75 25 19 4
0,1
Pass 2:
57 75 25 19 4
25 57 75 19 4
0,2
25 57 75 19 4
Pass 3:
19 25 57 75 4
0,3
4 19 25 57 75
Pass 4:
19 25 57 75 4 0,4
Thank You…!

More Related Content

What's hot (20)

Stack
StackStack
Stack
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
stack presentation
stack presentationstack presentation
stack presentation
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Sorting
SortingSorting
Sorting
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Array data structure
Array data structureArray data structure
Array data structure
 

Similar to sorting and its types

Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfSorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfMohammed472103
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxBubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxKalpana Mohan
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfarishmarketing21
 
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...Anwar Patel
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfeyewatchsystems
 
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. TrinidadLUISITO TRINIDAD
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Ra'Fat Al-Msie'deen
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method Shantanu Mishra
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppthenokmetaferia1
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 

Similar to sorting and its types (20)

Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfSorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdf
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxBubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptx
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
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...
 
my docoment
my docomentmy docoment
my docoment
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
 
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
 
Sorting
SortingSorting
Sorting
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Sorting
SortingSorting
Sorting
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

sorting and its types

  • 2. SORTING Sorting refers to operations of arranging a set of data in a given order. 10 20 30 40 50 60 30 10 60 20 50 40 Unsorted List Sorted List
  • 3. BASIC TYPES : Internal Sorting: If all the data to be sorted can be adjusted in main memory then it is called as Internal Sorting. External Sorting: If data to be stored is large and aquires external memory then the type is called as External Sorting.
  • 4. METHODS OF SORTING: Bubble Sort Selection Sort Insertion Sort
  • 6. ALGORITHM Bubble Sort: Algorithm of bubble sort includes two steps repeated until the list is sorted.  Compare adjacent elements, if the element on right side is smaller then swap their positions.  Compare first element, second element and so on on completion of Pass 1 the largest element is at last position.
  • 7. Pass 1: 50 10 30 20 40 50 10 30 20 40 10 50 30 20 40 10 30 50 20 40 10 30 20 50 40 10 30 20 40 50 0,1 1,2 2,3 3,4 Number Of Passes = Max – 1 = 5 – 1 = 4
  • 8. Pass 2: 10 30 20 40 50 10 30 20 40 50 10 30 20 40 50 10 30 20 40 50 0,1 1,2 2,3
  • 9. Pass 3: 10 30 20 40 50 10 30 20 40 50 10 30 20 40 50 0,1 1,2
  • 10. Pass 4: 10 30 20 40 50 0,1 10 30 20 40 50 10 30 20 40 50 Sorted list
  • 11. To sort numbers in ascending order using bubble sort technique import java.util.*; public class bubbleSort{ public static void main(String a[]) throws Exception { int i,j,n; int a[] = {50,10,30,20,40}; System.out.println("Values Before the sort:n"); for(i = 0; i < a.length; i++) System.out.print( a[i]+" "); System.out.println(); n=a.length;
  • 12. for(i = 1; i < n; i++) { for(j = 0; j < (n-i); j++) { if(a[j] > a[j+1]){ t = a[j]; a[j]=a[j+1]; a[j+1]=t; } } } System.out.print("Values after the sort:n"); for(i = 0; i <a.length; i++) System.out.print(a[i]+" "); } }
  • 14. ALGORITHM Selection Sort: Here in selection sort the algorithm depends on the zeroth element majorly.  The Zeroth element is compared with the first element and if the element at right is found smaller then their positions are swapped or exchanged.  The same procedure is carried with all elements of the list resulting into a fully sorted list.
  • 15. 23 15 29 11 1 23 15 29 11 1 Pass 1: Number Of Passes = Max – 1 = 5 – 1 = 4 15 23 29 11 1 15 23 29 11 1 11 23 29 15 1 1 23 29 15 11 0,1 0,2 0,3 0,4
  • 16. 1 23 29 15 11 Pass 2: 1 29 23 15 11 1 15 23 29 11 1 11 23 29 15 1,2 1,3 1,4
  • 17. 1 11 23 29 15 Pass 3: 1 11 23 29 15 1 11 15 29 23 2,3 2,4
  • 18. Pass 4: 1 11 15 29 23 1 11 15 23 29 1 11 15 23 29 Sorted List 3,4
  • 19. To sort numbers in ascending order using selection sort technique import java.util.*; public class selectionSort{ public static void main(String a[]) throws Exception { int i,j; int a[] = {23,15,29,11,1}; System.out.println("Values Before the sort:n"); for(i = 0; i < a.length; i++) System.out.print( a[i]+" "); System.out.println(); n=a.length;
  • 20. for(i = 0; i < n; i++) { for(j = i+1; j < n; j++) { if(a[i] > a[j]){ t = a[j]; a[j]=a[i]; a[i]=t; } } } System.out.print("Values after the sort:n"); for(i = 0; i <a.length; i++) System.out.print(a[i]+" "); }}
  • 22. Mathematical applications : in the search for greater value, or the smallest value. In many other applications. This method is effective when dealing with small numbers .
  • 23. ALGORITHM Insertion Sort In insertion sort the elements are compared and inserted to respective index place.  It starts with comparision of 1st and 0th element in pass 1.  In pass 2 the second element is compared with the 1st and 0th element.  Doing so with all the elements in the list appropriate element is inserted by shifting elements on right.
  • 24. public insertionSort( int [] a r r ) { f or ( i n t i = 1; i < arr.Length; ++i) { i n t temp = a r r [ i ] ; i n t pos = i ; } arr[pos] = temp; } } Select while (arr[pos-1].CompareTo(temp) > 0 &&pos > 0) { Comparing arr[pos] = arr[pos-1]; pos--; Shift Insert
  • 25. 75 57 25 19 4 Pass 1: 75 57 25 19 4 Number Of Passes = Max – 1 = 5 – 1 = 4 57 75 25 19 4 0,1
  • 26. Pass 2: 57 75 25 19 4 25 57 75 19 4 0,2
  • 27. 25 57 75 19 4 Pass 3: 19 25 57 75 4 0,3
  • 28. 4 19 25 57 75 Pass 4: 19 25 57 75 4 0,4