How to Quick Sort an Array in C++
Part 1 of 2:
Creating the quickSort Function
- Create the quickSort function. This is a recursive void function. It requires three parameters:
- The array (an int array)
- The left bound (an int variable)
- The right bound (an int variable; the size of the array subtracted by 1)
- Create the variables. These variables will be used to go through the list and to swap the values. Four variables are needed:
- An int i (the left bound)
- An int j (the right bound)
- An int temp (a temporary variable used for swapping without losing any data)
- An int pivot (the value of the middle point that splits the list to make it easier to sort)
- Create a while loop to begin sorting. A loop while i ≤ j is used to go through the indexes of the list. These values will be changed as the sublists that are being sorted change.
- Iterate through the left side. Another while loop checking if the element is less than pivot iterates through the list. If it is less than pivot value, increase i by 1. This checks if the left side of the sublist needs to be sorted.
- Iterate through the right side. Another while loop checking if the element is greater than pivot iterates through the list. If it is greater than pivot, decrease j by 1. This checks if the right side of the sublist needs to be sorted.
- Begin swapping the values if i ≤ j. Swapping the values of the list puts the values in ascending order. Assigning one value to another without a temporary variable will result in a loss of data. To avoid this, this procedure is used:
- Assign the value of the list at index i to temp.
- Assign the value of the list at index jto the list at index i.
- Assign temp to the list at index j.
- Add 1 to i.
- Subtract 1 from j.
- Check if each half of the list is sorted. This is done by two recursive calls. The first function call sorts the left sublist created by changing the bounds. When the left side is completely sorted, the next recursive call sorts the right sublist by changing its bounds.
- If left < j, call the function with left and i as the bounds.
- If right < i, call the function with i and right as the bounds.
Part 2 of 2:
Testing the quickSort Function
- Create the list in the main function. The array can be any size and can be initialized both explicitly and through other methods.
- Output the unsorted list using a for-loop. The bounds of the loop go from 0 to the sizeof(list)/4. This piece of code gives the number of elements in list.
- Call the quickSort function. The three needed parameters are:
- The list
- The left bound (0)
- The right bound (the size of the array subtracted by 1)
- Output the new list using a for-loop. Again, the bounds of the loop go from 0 to the sizeof(list)/4. This is because the sorted list contains the same amount of elements as the unsorted list (no data was lost).
- Run the program to see the sorted list. The number of items in list should be the same in both lists.
4.3 ★ | 3 Vote
You should read it
May be interested
- Sort the database in Excelinstructions on how to organize a database in excel. sorting data is indispensable when working in excel. 1. sort data simply. step 1: select the data range to be sorted - data - sort: step 2: sort dialog box appears field selection
- How to copy one array into another array in Golangin go, an array is a fixed-length sequence containing elements of a specific type. copying one array to another is simple but requires both arrays to be of the same length and type.
- Sort information on Facebookin the past, users could lose posts when they ignored the stream, especially if you gave facebook a large amount of time to access every day.
- Sort names alphabetically in Wordyou are asked by your boss to organize the list of employees alphabetically and not sure what to do? this article will show you how to sort names by abc
- Array formulas in Excel - Tutorials and examplesarray formulas in excel - tutorials and examples. an array formula is a formula that can perform multiple calculations of one or more items in an array. array formulas can return multiple values or a single result.
- Sort data in Excelsort data in excel - excel supports you with data sorting tools to give you the sorting options. you can use the sort tool to sort the data in your excel document.
- Learn about Collection of Record in JavaScriptin the previous article, we introduced you to a couple of 2-dimensional array features - 2d array in javascript, in many cases applied when we need to keep information about 1 or more lists of numbers of strings certain data, array object will be the most commonly used and most used tool ...
- Array (Array) in PHPan array is a data structure that stores one or more of the same value types in a single value.
- Instructions and examples of array formulas Array Formulas in Excelthe following article details array formulas and practical examples in excel to help you better understand and apply them in your work most effectively.
- Top 10 best array games for PCtop 10 best array games for pc you prefer to direct and command soldiers to occupy new bases and lands. simulate epic battles in history through strategy games, arrayed games. let's tipsmake.com find the top