SlideShare a Scribd company logo
1 of 29
Download to read offline
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

Similar to Sorting_Algoritm-computee-scienceggn.pdf

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
ย 
Chapter 14
Chapter 14Chapter 14
Chapter 14
Terry Yoast
ย 
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
aloeplusint
ย 
Selection sort
Selection sortSelection sort
Selection sort
Jay Patel
ย 
Im having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdfIm having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdf
stopgolook
ย 

Similar to Sorting_Algoritm-computee-scienceggn.pdf (20)

sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
ย 
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
ย 
9 Arrays
9 Arrays9 Arrays
9 Arrays
ย 
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
ย 
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"
ย 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
ย 
Code
CodeCode
Code
ย 
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
ย 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
ย 
Lecture12,13,14.pdf
Lecture12,13,14.pdfLecture12,13,14.pdf
Lecture12,13,14.pdf
ย 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
ย 
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
ย 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
ย 
Selection sort
Selection sortSelection sort
Selection sort
ย 
Sorting
SortingSorting
Sorting
ย 
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
ย 
Im having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdfIm having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdf
ย 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
ย 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
ย 

Recently uploaded

Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
home
ย 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
TusharBahuguna2
ย 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptx
jeswinjees
ย 
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
amitlee9823
ย 
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...
amitlee9823
ย 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
janettecruzeiro1
ย 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
suhanimunjal27
ย 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
amedia6
ย 
Government polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcdGovernment polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcd
shivubhavv
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
anilsa9823
ย 
Call Girls in Kalkaji Delhi 8264348440 call girls โค๏ธ
Call Girls in Kalkaji Delhi 8264348440 call girls โค๏ธCall Girls in Kalkaji Delhi 8264348440 call girls โค๏ธ
Call Girls in Kalkaji Delhi 8264348440 call girls โค๏ธ
soniya singh
ย 

Recently uploaded (20)

Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
ย 
Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance VVIP ๐ŸŽ SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance  VVIP ๐ŸŽ SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance  VVIP ๐ŸŽ SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance VVIP ๐ŸŽ SER...
ย 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
ย 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptx
ย 
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
ย 
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bang...
ย 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
ย 
๐Ÿ’ซโœ…jodhpur 24ร—7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
๐Ÿ’ซโœ…jodhpur 24ร—7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...๐Ÿ’ซโœ…jodhpur 24ร—7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
๐Ÿ’ซโœ…jodhpur 24ร—7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
ย 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
ย 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
ย 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
ย 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
ย 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
ย 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
ย 
Government polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcdGovernment polytechnic college-1.pptxabcd
Government polytechnic college-1.pptxabcd
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
ย 
Top Rated Pune Call Girls Koregaon Park โŸŸ 6297143586 โŸŸ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park โŸŸ 6297143586 โŸŸ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park โŸŸ 6297143586 โŸŸ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park โŸŸ 6297143586 โŸŸ Call Me For Genuine S...
ย 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
ย 
Call Girls in Kalkaji Delhi 8264348440 call girls โค๏ธ
Call Girls in Kalkaji Delhi 8264348440 call girls โค๏ธCall Girls in Kalkaji Delhi 8264348440 call girls โค๏ธ
Call Girls in Kalkaji Delhi 8264348440 call girls โค๏ธ
ย 

Sorting_Algoritm-computee-scienceggn.pdf

  • 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