SlideShare une entreprise Scribd logo
1  sur  15
GROUP 4
Objectives
Define sorting.
Types of sorting.
Selection Sort
Exchange Sort
Insertion Sort
What is Sorting ?
is the process of placing elements from a collection in some kind
of order.
refers to ordering data in an increasing or decreasing fashion
according to some linear relationship among the data items.
can be done on names, numbers and records. For example, it is
relatively easy to look up the phone number of a friend from a
telephone dictionary because the names in the phone book have
been sorted into alphabetical order.
Types of Sorting
Selection Sort
Exchange Sort
Insertion Sort
SELECTION SORT
In this method, sorting is done by inserting elements into
an existing sorted list. Initially, the sorted list has only one
element. Other elements are gradually added into the list
in the proper position.

In this technique, the first element is selected and
compared with all other elements. If any other element is
less than the first element swapping should take place. By
the end of this comparison, the least element most top
position in the array.
This is known as pass1. In pass II, the second element is
selected and compared with all other elements. Swapping
takes place if any other element is less than selected element.
This process continuous until array is sorted.
EXAMPLE :
9 2 5 7 4 8 on pass 1 look for smallest in 1st to 6th swap 2nd
with first giving
2 9 5 7 4 8 on pass 2 look for smallest in 2nd to 6th swap 5th
with second giving
2 4 5 7 9 8 on pass 3 look for smallest in 3rd to 6th swap 3rd
with third giving
2 4 5 7 9 8 on pass 4 look for smallest in 4th to 6th swap 4th
with fourth giving
2 4 5 7 9 8 on pass 5 look for smallest in 5th to 6th swap 5th
with 6th giving
2 4 5 7 8 9 sorted.
EXCHANGE SORT
The exchange sort is almost similar as the bubble sort. In fact
some people refer to the exchange sort as just a different bubble
sort. The exchange sort compares each element of an array and
swap those elements that are not in their proper position, just like
a bubble sort does. The only difference between the two sorting
algorithms is the manner in which they compare the elements.
The exchange sort compares the first element with each element
of the array, making a swap where is necessary.
EXAMPLE :

9 2 5 4 7 8 compare 1st & 2nd ->
2 9 5 4 7 8 compare 2nd & 3rd ->
2 5 9 4 7 8 compare 3rd & 4th ->
2 5 4 9 7 8 compare 4th & 5th ->
2 5 4 7 9 8 compare 5th & 6th ->
254789
1. Compare the first pair of numbers (positions 0 and 1) and
reverse them if they are not in the correct order.
2. Repeat for the next pair (positions 1 and 2).
3. Continue the process until all pairs have been checked.
4. Repeat steps 1 through 3 for positions 0 through n - 1 to i
(for i = 1, 2, 3, ...) until no pairs remain to be checked.
5. The list is now sorted.
INSERTION SORT
is an efficient algorithm for sorting a small number of
elements. Insertion sort works the same way as one would sort
a bridge or gin rummy hand, i.e. starting with an empty left
hand and the cards face down on the table. One card at a time
is then removed from the table and inserted into the correct
position in the left hand. To find the correct position for a card,
it is compared with each of the cards already in the hand,
from right to left.
12

Insertion Sort
To insert 12, we need to
make room for it by
moving first 36 and then
24.
13

Insertion Sort
14

Insertion Sort
Data Structures and Algorithms

Contenu connexe

Tendances (20)

Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Joins in databases
Joins in databases Joins in databases
Joins in databases
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
 
merge_sort
merge_sortmerge_sort
merge_sort
 
joins in database
 joins in database joins in database
joins in database
 
FRACTIONS
FRACTIONSFRACTIONS
FRACTIONS
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Joins in dbms and types
Joins in dbms and typesJoins in dbms and types
Joins in dbms and types
 
Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Server
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Math chapter 3 fractional exponent
Math chapter 3 fractional exponentMath chapter 3 fractional exponent
Math chapter 3 fractional exponent
 
Wednesdayweek7student
Wednesdayweek7studentWednesdayweek7student
Wednesdayweek7student
 
Join
JoinJoin
Join
 
Matrix
MatrixMatrix
Matrix
 
Unit v data structure-converted
Unit  v data structure-convertedUnit  v data structure-converted
Unit v data structure-converted
 
Sorting method data structure
Sorting method data structureSorting method data structure
Sorting method data structure
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 

En vedette

Sorting insertion-sort
Sorting   insertion-sortSorting   insertion-sort
Sorting insertion-sortFajar Zain
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sortingVivek Bhargav
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderAshin Guha Majumder
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
lecture 9
lecture 9lecture 9
lecture 9sajinsc
 
Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)Arvind Devaraj
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisAnindita Kundu
 
Sorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card SortingSorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card SortingStephen Anderson
 

En vedette (9)

Sorting insertion-sort
Sorting   insertion-sortSorting   insertion-sort
Sorting insertion-sort
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
lecture 9
lecture 9lecture 9
lecture 9
 
Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Sorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card SortingSorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card Sorting
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 

Similaire à Data Structures and Algorithms

Sorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxsubhanalichand514
 
Selection Sort and Insertion Sort
Selection Sort and Insertion SortSelection Sort and Insertion Sort
Selection Sort and Insertion Sorttamayaoraquel
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleVivek487417
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Selection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptxSelection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptxArjayBalberan1
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingnikshaikh786
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingThenmozhiK5
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)Neil Soliven
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptxDr.Shweta
 

Similaire à Data Structures and Algorithms (20)

Sorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptx
 
Selection Sort and Insertion Sort
Selection Sort and Insertion SortSelection Sort and Insertion Sort
Selection Sort and Insertion Sort
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Selection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptxSelection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptx
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 

Dernier

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
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.pdfQucHHunhnh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
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.docxRamakrishna Reddy Bijjam
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Dernier (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
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
 
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.
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

Data Structures and Algorithms

  • 2. Objectives Define sorting. Types of sorting. Selection Sort Exchange Sort Insertion Sort
  • 3. What is Sorting ? is the process of placing elements from a collection in some kind of order. refers to ordering data in an increasing or decreasing fashion according to some linear relationship among the data items. can be done on names, numbers and records. For example, it is relatively easy to look up the phone number of a friend from a telephone dictionary because the names in the phone book have been sorted into alphabetical order.
  • 4. Types of Sorting Selection Sort Exchange Sort Insertion Sort
  • 5. SELECTION SORT In this method, sorting is done by inserting elements into an existing sorted list. Initially, the sorted list has only one element. Other elements are gradually added into the list in the proper position. In this technique, the first element is selected and compared with all other elements. If any other element is less than the first element swapping should take place. By the end of this comparison, the least element most top position in the array.
  • 6. This is known as pass1. In pass II, the second element is selected and compared with all other elements. Swapping takes place if any other element is less than selected element. This process continuous until array is sorted.
  • 7. EXAMPLE : 9 2 5 7 4 8 on pass 1 look for smallest in 1st to 6th swap 2nd with first giving 2 9 5 7 4 8 on pass 2 look for smallest in 2nd to 6th swap 5th with second giving 2 4 5 7 9 8 on pass 3 look for smallest in 3rd to 6th swap 3rd with third giving 2 4 5 7 9 8 on pass 4 look for smallest in 4th to 6th swap 4th with fourth giving 2 4 5 7 9 8 on pass 5 look for smallest in 5th to 6th swap 5th with 6th giving 2 4 5 7 8 9 sorted.
  • 8. EXCHANGE SORT The exchange sort is almost similar as the bubble sort. In fact some people refer to the exchange sort as just a different bubble sort. The exchange sort compares each element of an array and swap those elements that are not in their proper position, just like a bubble sort does. The only difference between the two sorting algorithms is the manner in which they compare the elements. The exchange sort compares the first element with each element of the array, making a swap where is necessary.
  • 9. EXAMPLE : 9 2 5 4 7 8 compare 1st & 2nd -> 2 9 5 4 7 8 compare 2nd & 3rd -> 2 5 9 4 7 8 compare 3rd & 4th -> 2 5 4 9 7 8 compare 4th & 5th -> 2 5 4 7 9 8 compare 5th & 6th -> 254789
  • 10. 1. Compare the first pair of numbers (positions 0 and 1) and reverse them if they are not in the correct order. 2. Repeat for the next pair (positions 1 and 2). 3. Continue the process until all pairs have been checked. 4. Repeat steps 1 through 3 for positions 0 through n - 1 to i (for i = 1, 2, 3, ...) until no pairs remain to be checked. 5. The list is now sorted.
  • 11. INSERTION SORT is an efficient algorithm for sorting a small number of elements. Insertion sort works the same way as one would sort a bridge or gin rummy hand, i.e. starting with an empty left hand and the cards face down on the table. One card at a time is then removed from the table and inserted into the correct position in the left hand. To find the correct position for a card, it is compared with each of the cards already in the hand, from right to left.
  • 12. 12 Insertion Sort To insert 12, we need to make room for it by moving first 36 and then 24.