Any sequence of data of the same type can be represented as a list. Lists can be ordered and unordered. In the latter case, working with the data, finding the desired value, and accessing the elements of the list cause certain difficulties. The list of string variables is usually sorted alphabetically. There are many sorting methods, in each case you should choose the most optimal algorithm.
Instructions
Step 1
When choosing the best sorting method, there are two things to consider: the time taken for the sorting operation and the amount of memory required for auxiliary storage. Sorting algorithms that do not require additional memory are referred to as "in place" sorts. One of the easiest to implement is a slow bubble sort, which scans each pair of items in the list and swaps places depending on the desired order.
Step 2
There is a faster sorting method by finding the minimum or maximum item in the list. In the case of sorting alphabetically, each time you go through the list, you need to find its maximum element - this will be a string starting with the letter nearest to the beginning of the alphabet. Once found, the string is swapped with the very first item in the list on the first pass. Upon further consideration of the list, the first place is excluded, the next maximum element is sought, placed in second place, etc. The code of the sorting program in C ++ by the method of finding the maximum element of the list: String Arr [20], cTemp; int N = 20, Max, Pos; for (int i = 0; i <N-1; i ++) {Max = Arr ; Pos = i; for (int j = 0; j <N; j ++) {if (Arr [j] <Max) {Max = Arr [j]; Pos = j; } cTemp = Arr ; Arr = Arr [Pos]; Arr [Pos] = cTemp; }}
Step 3
The most optimal solution for ordering string data in a list is insertion sort. Its essence lies in the fact that with each pass through the list, there is an ordered part of the list of a certain number of elements, thus, the next element under consideration is placed in a suitable place in the list. C ++ code of the insertion sorting algorithm: String Arr [20], cTemp; int N = 20; for (int i = 1, j = 0; i <N; i ++) {cTemp = Arr ; j = i - 1; while (cTemp <Arr [j]) {Arr [j + 1] = Arr [j]; j--; if (j <0) break; Arr [j + 1] = cTemp; }}