1. Introduction
Quick Sort is a divide and conquer sorting algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.
2. Implementation Steps
1. Choose a 'pivot' element from the array and partition the other elements into two sub-arrays, according to whether they are less than or greater than the pivot.
2. Recursively apply the above steps to the sub-arrays.
3. The base case of the recursion is arrays of size 0 or 1, which never need to be sorted.
3. Implementation in Swift
func quickSort(_ arr: inout [Int], _ low: Int, _ high: Int) { if low < high { // Find pivot element such that element smaller than pivot are on the left // and elements greater than pivot are on the right let pivot = partition(&arr, low, high) // Recursively sort elements before and after pivot quickSort(&arr, low, pivot - 1) quickSort(&arr, pivot + 1, high) } } func partition(_ arr: inout [Int], _ low: Int, _ high: Int) -> Int { let pivot = arr[high] var i = low - 1 for j in low..<high { if arr[j] < pivot { i += 1 (arr[i], arr[j]) = (arr[j], arr[i]) } } (arr[i + 1], arr[high]) = (arr[high], arr[i + 1]) return i + 1 } // Usage: var numbers = [10, 80, 30, 90, 40, 50, 70] quickSort(&numbers, 0, numbers.count - 1) print(numbers)
Output:
[10, 30, 40, 50, 70, 80, 90]
Explanation:
1. The primary function quickSort begins by checking if the base case has been reached (i.e., if the segment of the array to be sorted contains one or zero elements). If not, it proceeds to partition the segment.
2. The partition function selects the last element as the pivot. All elements smaller than the pivot are moved to its left, and all the greater elements to its right.
3. The pivot is then placed in its correct position, which is the point where the array gets divided. The index of the pivot is returned.
4. The quickSort function is then recursively applied to the sub-arrays formed by dividing the array at the pivot.
Note: The choice of the pivot selection and partitioning schemes greatly affects the algorithm's performance. This simple version always picks the last element as a pivot, which works well with many real-world data sets.
DSA Related Swift Examples:
Swift Hello World Program Swift Program to Add Two Numbers Swift Program to Subtract Two Numbers Swift Program to Multiply Two Numbers Swift Program to Divide Two Numbers Swift Program to Find Remainder Swift Program to Check Even or Odd Swift Program to Find Factorial of a Number Swift Program to Generate Fibonacci Series Swift Program to Swap Two Numbers Without Using Temporary Variable Swift Program to Find Largest Among Three Numbers Swift Program to Calculate the Area of a Circle Swift Program to Reverse a Number Swift Program to Make a Simple Calculator Swift Program to Check Palindrome Swift Program to Count Number of Digits in an Integer Swift Program to Sum of Natural Numbers Swift Program to Display Times Table Swift Program to Check Prime Number Swift Program to Find LCM Swift Program to Find GCD Swift Program to Find the Power of a Number Swift Program to Split a String into Words Swift Program to Check Leap Year Swift Program to Join Two Strings Swift Program to Check Armstrong Number Swift Program to Find Sum of Array Elements Swift Program to Find the Largest Element of an Array Swift Program to Perform Matrix Addition Swift Program to Transpose a Matrix Swift Program to Multiply Two Matrices Swift Program to Find Length of a String Swift Program to Copy One String to Another String Swift Program to Concatenate Two Strings Swift Program to Search for a Character in a String Swift Program to Count Frequency of a Character in String Swift Program to Create a Simple Class and Object Swift Program to Implement Inheritance Swift Program to Handle Simple Exceptions Swift Variables and Constants Example Swift Data Types (Int, Double, String) Example Swift Optionals and Optional Binding Example Swift Tuples Example Swift Array Example Swift Dictionary Example Swift Set Example Swift Closures Example Swift Enums Example Swift Structures Example Swift Properties (Stored, Computed) Example Swift Methods (Instance, Type) Example Swift Subscripts Example Swift Inheritance and Overriding Example Swift Protocols Example Swift Extensions Example Swift Generics and Generic Functions Example Swift Error Handling with Do-Catch Example Swift Guard Statement Example Swift Defer Statement Example Swift Type Casting (as, is, as?) Example Swift Access Control Example Swift Attributes (@available, @discardableResult) Example Swift Pattern Matching Example Swift Switch Statement and Cases Example Swift For-In Loop Example Swift While and Repeat-While Loops Example Swift Conditional Statements (If, If-Else, Ternary) Example Swift Operators Example Swift Memory Management Example Swift Strong, Weak, and Unowned References Example Swift Initialization and Deinitialization Example Swift Protocol-Oriented Programming Example Swift Nested Types Example Swift Type Aliases Example Swift Dynamic Member Lookup Example Swift Lazy Stored Properties Example Swift KeyPaths Example Swift String Manipulation and Methods Example Swift Regular Expressions Example Swift
Comments
Post a Comment