[Data structure] Select Sorting Algorithm example and data structure algorithm example

Source: Internet
Author: User

[Data structure] Select Sorting Algorithm example and data structure algorithm example
Select the sort and edit sort algorithm to solve the following problems: Enter the number of n sequences <a1, a2, a3,..., an>. Output a rearrangement of the original sequence <a1 *, a2 *, a3 *,..., an *>; To make a1 * <= a2 * <= a3 * <=... <= an * There are many sorting algorithms, including insertion sorting, Bubble sorting, heap sorting, Merge Sorting, selection sorting, Count sorting, base sorting, bucket sorting, and quick sorting. Insert sorting, heap sorting, select sorting, Merge Sorting, and quick sorting, and Bubble sorting are both compared and sorted by comparing the elements in the array, other sorting algorithms use non-comparative methods to obtain sorting information about input arrays. The Direct selection and sorting of the files with n records can be directly selected and sorted through n-1. The result is as follows: ① initial state: the unordered area is R [1 .. n], the ordered area is empty. ② Sorting 1st bits in the disordered zone R [1 .. n] selects the record R [k] with the minimum keyword, swaps it with the 1st records R [1] In the unordered area, so that R [1 .. 1] and R [2 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count. ...... ③ At the beginning of the I sort, the current ordered area and disordered area are R [1 .. I-1] and R (I.. n) respectively ). This sort field selects the record R [k] with the smallest keyword from the current unordered area and exchanges it with the R of the 1st records in the unordered area so that R [1 .. i] and R change to a new ordered area with one more record count and a new unordered area with one fewer record count. The general interpretation compares the size of the first element and the last element in the array. If the latter element is smaller than the former element, a variable k is used to remember its position, and then the second comparison is performed, the previous "last element" is now "previous element ", continue to compare with his "next element". If the following element is smaller than him, use the variable k to remember its position in the array (subscript). When the loop ends, we should find the subscript of the smallest number, and then make a judgment. If the subscript of this element is not the subscript of the first element, let the first element exchange the value with him, in this way, we can find the smallest number in the entire array. Then find the second small number in the array, and let him swap the value with the second element in the array, and so on. Code implementation:

# Include <iostream> # include <cstdio> # include <cstdlib> # include <ctime> # define SIZE 15 using namespace std; void selectSort (int * a, int len) {int h; int tmp; for (int I = 0; I <SIZE-1; I ++) {int k = I; for (int j = I + 1; j <len; j ++) {if (a [j] <a [k]) k = j;} if (k! = I) {tmp = a [I]; a [I] = a [k]; a [k] = tmp ;} cout <"no." <I <"Result:"; for (int h = 0; h <len; h ++) {cout <a [h] <";}cout <endl ;}int main () {int array [SIZE]; srand (time (NULL )); for (int I = 0; I <SIZE; I ++) {array [I] = rand ()/1000 + 100 ;}cout <"the sequence before sorting is: "<endl; for (int I = 0; I <SIZE; I ++) {cout <array [I] <"";} cout <endl; selectSort (array, SIZE); cout <endl; cout <"the sorted sequence is:" <endl; for (int I = 0; I <SIZE; I ++) {cout <array [I] <"" ;}return 0 ;}

 

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.