Tag Archives: sort

C++ || How To Sort An Array/Vector/Container With Multiple Sorting Conditions Using C++

The following is a module with functions which demonstrates how to sort an array/vector/container with multiple sorting conditions using C++.

The function demonstrated on this page is a wrapper for the std::sort function, which means it works for any container supported by std::sort.

This function accepts multiple sorting conditions (comparison functions), which allows for complex array sorting much like the SQL/LINQ ‘Order By’ operation.


1. Simple Array – Ascending

The example below demonstrates the use of Utils::sortBy to sort a simple array.

In this example, no sorting conditions are specified. In this case, the array is sorted in ascending order.


2. Simple Array – Descending

The example below demonstrates the use of Utils::sortBy to sort a simple array.

In this example, a sorting condition (comparison function) is specified. In this case, the array is sorted in descending order.

Note: In the example below, a lambda is used, though it is not required. A regular function can be used here as well.


3. Object Vector – Multi Key Sort

The example below demonstrates the use of Utils::sortBy to sort an object vector.

In this example, multiple sorting conditions (comparison functions) are specified. In this case, the vector is sorted by multiple object properties.

When multiple sorting conditions are specified, the sequence is sorted in the order in which the conditions are supplied (FIFO).

Note: In the example below, lambda functions are used, though they are not required. Regular functions can be used here as well.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || Snippet – How To Find The Minimum & Maximum Of 3 Numbers, Print In Ascending Order

This page will demonstrate how to find the minimum and maximum of 3 numbers. After the maximum and minimum numbers are obtained, the 3 numbers are displayed to the screen in ascending order.

This program uses multiple if-statements to determine equality, and uses 3 seperate int varables to store its data. This program is very basic, so it does not utilize an integer array, or any sorting methods.

NOTE: If you want to find the Minimum & Maximum of numbers contained in an integer array, click here.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Please enter 3 numbers: 89 56 1987
The numbers you just entered are: 89 56 1987

The maximum number is: 1987
The minimum number is: 56
The numbers in order are: 56 89 1987

C++ || “One Size Fits All” – BubbleSort Which Works For Integer, Float, & Char Arrays

Here is another sorting algorithm, which sorts data that is contained in an array. The difference between this method, and the previous methods of sorting data is that this method works with multiple data types, all in one function. So for example, if you wanted to sort an integer and a character array within the same program, the code demonstrated on this page has the ability to sort both data types, eliminating the need to make two separate sorting functions for the two different data types.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Integer Arrays
Character Arrays
BubbleSort
Pointers
Function Pointers - What Are They?
Memcpy
Sizeof
Sizet
Malloc


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Notice, the same function declaration is being used for all 3 different data types, with the only difference between each function call are the parameters which are being sent out.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: The function works for all three data types)


Original values in the int array:
44 91 43 22 20 100 77 80 84 60 47 91 51 81

The sorted values in the int array:
20 22 43 44 47 51 60 77 80 81 84 91 91 100
---------------------------------------------------------------------------------------
Original values in the float array:
49.5 30.5 67.5 50.5 29.5 89.5 78.5 80.5 54.5 7.5 54.5 38.5 56.5 70.5

The sorted values in the float array:
7.5 29.5 30.5 38.5 49.5 50.5 54.5 54.5 56.5 67.5 70.5 78.5 80.5 89.5
---------------------------------------------------------------------------------------
Original values in the char array:
This Is Random Text Brought To You By My Programming Notes

The sorted values in the char array:
Brought By Is My Notes Programming Random Text This To You

C++ || Snippet – Bubble Sort, Selection Sort, Insertion Sort, Quick Sort & Merge Sort Sample Code For Integer Arrays

This page consists of algorithms for sorting integer arrays. Highlighted on this page are Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, and Merge Sort.

In terms of performance and speed, the sorting algorithms on this page will be listed from the (on average) worst, to best case implementations.

Selection sort and Insertion sort are two simple sorting algorithms which are often more efficient than Bubble Sort, though all three techniques aren’t the top of the class algorithmically for sorting large data sets.

====== BUBBLE SORT ======

SAMPLE OUTPUT:

Original array values
91 65 53 93 54 41 69 76 55 90 10 62
--------------------------------------------------------
The current sorted array
10 41 53 54 55 62 65 69 76 90 91 93

====== SELECTION SORT ======

SAMPLE OUTPUT:

Original array values
87 74 58 64 4 43 23 16 3 93 9 80
--------------------------------------------------------
The current sorted array
3 4 9 16 23 43 58 64 74 80 87 93

====== INSERTION SORT ======

SAMPLE OUTPUT:

Original array values
97 80 94 74 10 38 87 7 87 14 3 97
--------------------------------------------------------
The current sorted array
3 7 10 14 38 74 80 87 87 94 97 97

====== QUICK SORT ======

Quicksort is one of the fastest sorting algorithms, and is often the best practical choice for sorting, as its average expected running time for large data sets is more efficient than the previously discussed methods.

SAMPLE OUTPUT:

Original array values
50 94 1 16 51 63 41 17 70 28 6 34
--------------------------------------------------------
The current sorted array
1 6 16 17 28 34 41 50 51 63 70 94

====== MERGE SORT ======

Merge sort is a fast, stable sorting routine which, in the worst case, does about (on average) 39% fewer comparisons than quick sort.

SAMPLE OUTPUT:

Original array values
18 46 41 30 84 97 54 49 19 32 70 30
--------------------------------------------------------
The current sorted array
18 19 30 30 32 41 46 49 54 70 84 97

C++ || Snippet – Sort An Integer Array Using Bubble Sort – Print Each Pass & Total Number Of Swaps

This is a program which has no functionality, but displays the sorting of an integer array through the use of the bubble sort algorithm.

This program sorts the values of a one-dimensional array in ascending order using bubble sort. It also prints the total number of passes thru each iteration

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Integer Arrays
Bubble Sort
Setw

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Original array values
0 1 3 95 2 4 6 10 15 4 17 35
---------------------------------------------------------------
Array after bubble sort pass #1
0 1 3 2 4 6 10 15 4 17 35 95

Array after bubble sort pass #2
0 1 2 3 4 6 10 4 15 17 35 95

Array after bubble sort pass #3
0 1 2 3 4 6 4 10 15 17 35 95

Array after bubble sort pass #4
0 1 2 3 4 4 6 10 15 17 35 95
---------------------------------------------------------------
The current sorted array
0 1 2 3 4 4 6 10 15 17 35 95
---------------------------------------------------------------
Total Swaps: 12
Total Passes: 4