Sorting, reverse, half-lookup, and insertion of Arrays

Source: Internet
Author: User

Note the following when using Arrays:
1. subscripts out of bounds;
2. When operating the array, you must first analyze it and have a general idea. Select sorting, Bubble sorting, and quick sorting. The quick sorting code is more complex. Next we will focus on the quick sorting (reference part of Baidu's content)

The quick sorting algorithm is as follows:

1) set two variables I, j, sorting start: I = 0, j = N-1; 2) the first array element as the key data, assigned to the key, that is, key = A [0]; 3) search forward from J, that is, search forward from the back (j = J-1), find the first value less than key a [J], and exchange with the key; 4) Start from I and search backward, that is, start from the beginning to search backward (I = I + 1), find the first a [I] greater than the key, exchange with key; 5) Repeat steps 3rd, 4, 5 until I = J; (Step 4 is not found in the program when J = J-1, I = I + 1, until it is found. When I is found and switched, the position of the J pointer remains unchanged. In addition, when I = J, this process must be the end of the last loop completed by I + or J .) For example, the values of array a to be sorted are as follows: (initial key data: x = 49) Note that key x remains unchanged and will always be compared with X, No matter what position, the final goal is to put X in the middle and a small one in front and a large one in the back. A [0] a [1] A [2] a [3] a [4] a [5] a [6]: 49 38 65 97 76 13 27 after the first exchange: 27 38 65 97 76 13 49 (based on the third step of the algorithm, start later) after the second exchange: 27 38 49 97 76 13 65 (according to the fourth step of the algorithm, find the value> X, 65> 49, and exchange the two. At this time: I = 3) after the third exchange: 27 38 13 97 76 49 65: 27 38 13 49 76 97 65 (according to the fourth step of the algorithm, find a value greater than X from the beginning, 97> 49, the two exchange, at this time: I = 4, j = 6) at this time, we will find that I = J and end the fast sorting. After a fast sorting, the result is: 27 38 13 49 76 97 65, that is, all the numbers greater than 49 are all behind 49, and all the numbers smaller than 49 are all before 49.

Graph Analysis:

Code:

Public class mysort {/*** array selection sorting, Bubble sorting, fast sorting, reverse, half-lookup, insert * @ author Acer */public static void main (string [] ARGs) {int arr [] = {-,-9, 18}; // selectsort (ARR); // show (ARR); // bubblesort (ARR ); // show (ARR); // quicksort (ARR, 0, arr. length-1); // show (ARR); // reversearray (ARR); // show (ARR); // quicksort (ARR, 0, arr. length-1); // sort to ensure the order of the array // show (ARR); // int Index = search (ARR, 10); // system. out. println (INDEX); quicksort (ARR, 0, arr. length-1); // sort to ensure the orderly show (insert (ARR, 12) of the array;}/* select sorting: select one element for each comparison and compare it with other elements one by one, each trip gets a sequence with the highest value in the front, increasing from left to right */public static void selectsort (INT arr []) {for (INT I = 0; I <arr. length-1; I ++) {for (Int J = I + 1; j <arr. length; j ++) {If (ARR [I]> arr [J]) {swap (ARR, I, j ); // Switch location }}/// extract the code for switching two element locations from the public static void swap (INT arr [], int A, int B) {int temp = 0; temp = arr [a]; arr [a] = arr [B]; arr [B] = temp;} // in each loop Sort the obtained value to the end of the public static void bubblesort (INT arr []) {for (INT I = 0; I <arr. length-1; I ++) {//-1 is used to prevent the array subscript from crossing the border.-I is used to reduce the number of comparisons for (Int J = 0; j <arr. length-1-i; j ++) {If (ARR [J]> arr [J + 1]) {swap (ARR, J, J + 1 ); // switching location }}// fast sorting efficiency relatively high public static void quicksort (INT arr [], int left, int right) {If (left> = right) {return;} int low = left, high = right; int low = arr [low]; // set the number of pivot while (low 

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.