SlideShare a Scribd company logo
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

Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structureghhgj jhgh
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
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
03446940736
 
Sorting
SortingSorting
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 

What's hot (20)

Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
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
 
Sorting
SortingSorting
Sorting
 
stack & queue
stack & queuestack & queue
stack & queue
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
single linked list
single linked listsingle linked list
single linked list
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Selection sort
Selection sortSelection sort
Selection sort
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Python tuple
Python   tuplePython   tuple
Python tuple
 

Similar to sorting and its types

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
 
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
arishmarketing21
 
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
 
my docoment
my docomentmy docoment
my docoment
NeeshanYonzan
 
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
eyewatchsystems
 
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
LUISITO TRINIDAD
 
Sorting
SortingSorting
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
 
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
 
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
SortingSorting
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).ppt
henokmetaferia1
 
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
Shanmuganathan C
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
Vineeta Garg
 

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
 
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
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
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
 
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++
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 

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