SlideShare uma empresa Scribd logo
1 de 10
CHAPTER 6 Sorting and Selection
Algorithm 6.1.2 Insertion Sort This algorithm sorts the array  a  by first inserting  a [2] into the sorted array  a [1]; next inserting  a [3] into the sorted array  a [1],  a [2]; and so on; and finally inserting  a [ n ] into the sorted array  a [1], ... ,  a [ n  - 1]. Input Parameters:  a Output Parameters: None insertion_sort ( a ) { n  =  a . last for  i  = 2 to  n  { val  =  a [ i ]  // save  a [ i ] so it can be inserted  j  =  i  – 1 // into the correct place // if  val  <  a [ j ],move  a [ j ] right to make room for  a [ i ] while ( j  ≥ 1 &&  val  <  a [ j ]) { a [ j  + 1] =  a [ j ] j  =  j  - 1 } a [ j  + 1] =  val  // insert  val } }
Algorithm 6.2.2 Partition This algorithm partitions the array a [ i ], ... ,  a [ j ] by inserting  val   =  a [ i ] at the index  h  where it would be if the array was sorted. When the algorithm concludes, values at indexes less than  h  are less than  val , and values at indexes greater than  h  are greater than or equal to  val . The algorithm returns the index  h . Input Parameters:  a , i , j Output Parameters:  i partition ( a , i , j ) { val  =  a [ i ] h  =  i for  k  =  i  + 1 to  j if ( a [ k ] <  val ) { h  =  h  + 1 swap ( a [ h ], a [ k ]) } swap  ( a [ i ], a [ h ]) return  h }
Algorithm 6.2.4 Quicksort This algorithm sorts the array a [ i ], ... ,  a [ j ] by using the partition algorithm (Algorithm 6.2.2). Input Parameters:  a , i , j Output Parameters:  i quicksort ( a , i , j ) { if ( i  <  j ) { p  =  partition ( a , i , j ) quicksort ( a , i , p  - 1) quicksort ( a , p  + 1, j ) } }
Algorithm 6.2.6 Random Partition This algorithm partitions the array a [ i ], ... ,  a [ j ] by inserting  val   =  a [ i ] at the index  h  where it would be if the array was sorted. The index  k  is chosen randomly. We assume that the function rand ( i , j ) executes in constant time and returns a random integer between  i  and  j  , inclusive. After inserting  val  at index  h , values at indexes less than  h  are less than  val , and values at indexes greater than  h  are greater than or equal to  val . The algorithm returns the index  h . Input Parameters:  a , i , j Output Parameters:  i random_partition ( a , i , j ) { k  =  rand [ i , j ] swap  ( a [ i ], a [ k ]) return  partition ( i , j ) // Algorithm 6.2.2 }
Algorithm 6.2.7 Random Quicksort This algorithm sorts the array a [ i ], ... ,  a [ j ] by using the random partition algorithm (Algorithm 6.2.6). Input Parameters:  a , i , j Output Parameters:  i random _ quicksort ( a , i , j ) { if ( i  <  j ) { p  =  random _ partition ( a , i , j ) random _ quicksort ( a , i , p  - 1) random _ quicksort ( a , p  + 1, j ) } }
Algorithm 6.4.2 Counting Sort This algorithm sorts the array a [1], ... ,  a [ n ] of integers, each in the range 0 to  m , inclusive.
Input Parameters:  a , m Output Parameters:  a counting_sort ( a , m ) { // set  c [ k ] = the number of occurrences of value  k   // in the array  a . // begin by initializing  c  to zero. for  k  = 0 to  m c [ k ] = 0 n  =  a . last for  i  = 1 to  n c [ a [ i ]] =  c [ a [ i ]] + 1 // modify  c  so that  c [ k ] = number of elements ≤  k for k = 1 to  m c [ k ] =  c [ k ] +  c [ k  - 1] // sort a with the result in  b for  i  =  n  downto 1 { b [ c [ a [ i ]]] =  a [ i ]   c [ a [ i ]] =  c [ a [ i ]] - 1 } // copy  b  back to  a for  i  = 1 to  n a [ i ] =  b [ i ] }
Algorithm 6.4.4 Radix Sort This algorithm sorts the array a [1], ... ,  a [ n ] of integers. Each integer has at most  k  digits. Input Parameters:  a , k Output Parameters:  a radix_sort ( a , k ) { for  i  = 0 to  k  - 1 counting_sort ( a ,10) // key is digit in 10 i ’s place }
Algorithm 6.5.2 Random Select Let  val  be the value in the array  a [ i ], ... ,  a [ j ] that would be at index  k  ( i   ≤   k   ≤   j  ) if the entire array was sorted. This algorithm rearranges the array so that  val  is at index  k , all values at indexes less than  k  are less than  val , and all values at indexes greater than  k  are greater than or equal to  val . The algorithm uses the random-partition algorithm (Algorithm 6.2.6). Input Parameters:  a , i , j , k Output Parameter:  a random_select ( a , i , j , k ) { if ( i  <  j ) { p  =  random_partition ( a , i , j ) if ( k  ==  p ) return if ( k  <  p ) random_select ( a , i , p  - 1, k ) else random_select ( a , p  + 1, j , k ) } }

Mais conteúdo relacionado

Mais procurados

Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaPriyanka Rana
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queuezzzubair
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmKrupali Mistry
 
Linear Regression Parameters
Linear Regression ParametersLinear Regression Parameters
Linear Regression Parameterscamposer
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queueSmit Parikh
 
A sorted linear array
A sorted linear array A sorted linear array
A sorted linear array Suneel Dogra
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical ComputingNaveed Rehman
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Algebraic Approach to Implementing an ATL Model Checker
Algebraic Approach to Implementing an ATL Model CheckerAlgebraic Approach to Implementing an ATL Model Checker
Algebraic Approach to Implementing an ATL Model Checkerinfopapers
 

Mais procurados (18)

Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
Lec3
Lec3Lec3
Lec3
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Greedy method
Greedy method Greedy method
Greedy method
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision Algorithm
 
Queue
QueueQueue
Queue
 
Linear Regression Parameters
Linear Regression ParametersLinear Regression Parameters
Linear Regression Parameters
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
 
A sorted linear array
A sorted linear array A sorted linear array
A sorted linear array
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Algebraic Approach to Implementing an ATL Model Checker
Algebraic Approach to Implementing an ATL Model CheckerAlgebraic Approach to Implementing an ATL Model Checker
Algebraic Approach to Implementing an ATL Model Checker
 
R: Apply Functions
R: Apply FunctionsR: Apply Functions
R: Apply Functions
 

Destaque

Destaque (20)

Ch05 Black Jack
Ch05  Black  JackCh05  Black  Jack
Ch05 Black Jack
 
Lecture4
Lecture4Lecture4
Lecture4
 
Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)Intersection Study - Algorithm(Sort)
Intersection Study - Algorithm(Sort)
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
04 ds and algorithm session_05
04 ds and algorithm session_0504 ds and algorithm session_05
04 ds and algorithm session_05
 
Bubble sort algorithm
Bubble sort algorithmBubble sort algorithm
Bubble sort algorithm
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Selection sort
Selection sortSelection sort
Selection sort
 
Quicksort
QuicksortQuicksort
Quicksort
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Sorting
SortingSorting
Sorting
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic Sorting
 

Semelhante a Chap06alg

Semelhante a Chap06alg (20)

Chap08alg
Chap08algChap08alg
Chap08alg
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Chap12alg
Chap12algChap12alg
Chap12alg
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine Matrices
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
Arrays
ArraysArrays
Arrays
 
Chap11alg
Chap11algChap11alg
Chap11alg
 
Chap11alg
Chap11algChap11alg
Chap11alg
 
Array
ArrayArray
Array
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Chap07alg
Chap07algChap07alg
Chap07alg
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 

Mais de Munkhchimeg (20)

Protsesor
ProtsesorProtsesor
Protsesor
 
Lecture916
Lecture916Lecture916
Lecture916
 
Lecture915
Lecture915Lecture915
Lecture915
 
Lecture914
Lecture914Lecture914
Lecture914
 
Lecture913
Lecture913Lecture913
Lecture913
 
Lecture911
Lecture911Lecture911
Lecture911
 
Lecture912
Lecture912Lecture912
Lecture912
 
Lecture910
Lecture910Lecture910
Lecture910
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture9
Lecture9Lecture9
Lecture9
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture7
Lecture7Lecture7
Lecture7
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture3
Lecture3Lecture3
Lecture3
 
Ded Algorithm
Ded AlgorithmDed Algorithm
Ded Algorithm
 
Ded Algorithm1
Ded Algorithm1Ded Algorithm1
Ded Algorithm1
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 
Lecture914
Lecture914Lecture914
Lecture914
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 

Último

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 

Último (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 

Chap06alg

  • 1. CHAPTER 6 Sorting and Selection
  • 2. Algorithm 6.1.2 Insertion Sort This algorithm sorts the array a by first inserting a [2] into the sorted array a [1]; next inserting a [3] into the sorted array a [1], a [2]; and so on; and finally inserting a [ n ] into the sorted array a [1], ... , a [ n - 1]. Input Parameters: a Output Parameters: None insertion_sort ( a ) { n = a . last for i = 2 to n { val = a [ i ] // save a [ i ] so it can be inserted j = i – 1 // into the correct place // if val < a [ j ],move a [ j ] right to make room for a [ i ] while ( j ≥ 1 && val < a [ j ]) { a [ j + 1] = a [ j ] j = j - 1 } a [ j + 1] = val // insert val } }
  • 3. Algorithm 6.2.2 Partition This algorithm partitions the array a [ i ], ... , a [ j ] by inserting val = a [ i ] at the index h where it would be if the array was sorted. When the algorithm concludes, values at indexes less than h are less than val , and values at indexes greater than h are greater than or equal to val . The algorithm returns the index h . Input Parameters: a , i , j Output Parameters: i partition ( a , i , j ) { val = a [ i ] h = i for k = i + 1 to j if ( a [ k ] < val ) { h = h + 1 swap ( a [ h ], a [ k ]) } swap ( a [ i ], a [ h ]) return h }
  • 4. Algorithm 6.2.4 Quicksort This algorithm sorts the array a [ i ], ... , a [ j ] by using the partition algorithm (Algorithm 6.2.2). Input Parameters: a , i , j Output Parameters: i quicksort ( a , i , j ) { if ( i < j ) { p = partition ( a , i , j ) quicksort ( a , i , p - 1) quicksort ( a , p + 1, j ) } }
  • 5. Algorithm 6.2.6 Random Partition This algorithm partitions the array a [ i ], ... , a [ j ] by inserting val = a [ i ] at the index h where it would be if the array was sorted. The index k is chosen randomly. We assume that the function rand ( i , j ) executes in constant time and returns a random integer between i and j , inclusive. After inserting val at index h , values at indexes less than h are less than val , and values at indexes greater than h are greater than or equal to val . The algorithm returns the index h . Input Parameters: a , i , j Output Parameters: i random_partition ( a , i , j ) { k = rand [ i , j ] swap ( a [ i ], a [ k ]) return partition ( i , j ) // Algorithm 6.2.2 }
  • 6. Algorithm 6.2.7 Random Quicksort This algorithm sorts the array a [ i ], ... , a [ j ] by using the random partition algorithm (Algorithm 6.2.6). Input Parameters: a , i , j Output Parameters: i random _ quicksort ( a , i , j ) { if ( i < j ) { p = random _ partition ( a , i , j ) random _ quicksort ( a , i , p - 1) random _ quicksort ( a , p + 1, j ) } }
  • 7. Algorithm 6.4.2 Counting Sort This algorithm sorts the array a [1], ... , a [ n ] of integers, each in the range 0 to m , inclusive.
  • 8. Input Parameters: a , m Output Parameters: a counting_sort ( a , m ) { // set c [ k ] = the number of occurrences of value k // in the array a . // begin by initializing c to zero. for k = 0 to m c [ k ] = 0 n = a . last for i = 1 to n c [ a [ i ]] = c [ a [ i ]] + 1 // modify c so that c [ k ] = number of elements ≤ k for k = 1 to m c [ k ] = c [ k ] + c [ k - 1] // sort a with the result in b for i = n downto 1 { b [ c [ a [ i ]]] = a [ i ] c [ a [ i ]] = c [ a [ i ]] - 1 } // copy b back to a for i = 1 to n a [ i ] = b [ i ] }
  • 9. Algorithm 6.4.4 Radix Sort This algorithm sorts the array a [1], ... , a [ n ] of integers. Each integer has at most k digits. Input Parameters: a , k Output Parameters: a radix_sort ( a , k ) { for i = 0 to k - 1 counting_sort ( a ,10) // key is digit in 10 i ’s place }
  • 10. Algorithm 6.5.2 Random Select Let val be the value in the array a [ i ], ... , a [ j ] that would be at index k ( i ≤ k ≤ j ) if the entire array was sorted. This algorithm rearranges the array so that val is at index k , all values at indexes less than k are less than val , and all values at indexes greater than k are greater than or equal to val . The algorithm uses the random-partition algorithm (Algorithm 6.2.6). Input Parameters: a , i , j , k Output Parameter: a random_select ( a , i , j , k ) { if ( i < j ) { p = random_partition ( a , i , j ) if ( k == p ) return if ( k < p ) random_select ( a , i , p - 1, k ) else random_select ( a , p + 1, j , k ) } }