< Programming Fundamentals
Overview
A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order and lexicographical order.[1] Most current programming languages include built-in or standard library functions for sorting arrays.
Discussion
Sorting is the process through which data are arranged according to their values. The following examples show standard library and/or built-in array sorting methods for different programming languages.
Language | Sort Example |
---|---|
C++ | #include <algorithm> sort(array, array + sizeof(array) / sizeof(int)); |
C# | System.Array.Sort(array); |
Java | import java.util.Arrays; Arrays.sort(array); |
JavaScript | array.sort(); |
Python | array.sort() |
Swift | array.sort() |
Language | Reverse Sort Example |
---|---|
C++ | #include <algorithm> sort(array, array + sizeof(array) / sizeof(int), greater <int>()); |
C# | System.Array.Sort(array); System.Array.Reverse(array); |
Java | import java.util.Arrays; Arrays.sort(array, Collections.reverseOrder()); |
JavaScript | array.sort; array.reverse(); |
Python | array.sort(reverse=True) |
Swift | array.sort(>) |
Key Terms
- sorting
- Arranging data according to their values.
References
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.