Java Algorithm Analysis 2 ————— several sort & Hanoi algorithms

Source: Internet
Author: User

One: Insert sort

/* * Insert sort *//* * Original sequence [9] 6  * No. 0 Trip [9] 6 *  1th trip [9] 6 9  * 2nd trip [12] 15 6  * 3rd trip [6 9]  the number of * n, how many times does it take? N number, n-1 trip  * No. 0 trip, the number of 1 positions, and the number of 1 positions before the comparison, in order of the size  of the 1th trip, the number of 2 positions, and 2 positions before the number of comparisons, in order  of size ... * I trip, compare the number of i+1 positions, and the number before I position, arranged in order of size.  */public class sort01 {public static void main (string[] args) {int[] num = {3, 4, 1, 6, 3};for (int i = 0; i < n Um.length-1; i++) {for (int j = 0; J < i + 1; j + +) {//I trip ... Compare the number of i+1 positions to the number before the i+1 position//Judging the current number num[j] vs num[i+1] if (num[i + 1] < num[j]) {int temp = num[i + 1];num[i + 1] = Num[j];nu M[J] = temp;}}} for (int i = 0; I <= num.length-1; i++) {System.out.println ("After sorting completed:" + Num[i]);}}}


Two: Select sort

/* * Select Sort *//* * The following is a simple selection of the stored state of the sort, where the curly braces are unordered, outside the curly brace for an ordered sequence: * Initial sequence: {* * *  * * For the 1th trip: 12 and 49 Exchange: 12 {27 65 97 76 4  9}  * 2nd trip: 27 Do not move: {$ 97)  * 3rd trip: 65 and 38 Exchange:  * 4th trip: 49 and 12 Exchange: 27 38 49 {  * 5th trip: 76 and 65 Exchange: A. ($)  * 6th trip: 97 and 76 Exchange:------  Ublic static void Main (string[] args) {int[] num = {2, 5, 3, 6, 9,};for (int i = 0; I <= num.length-1; i++) {//Compare n round//find min int min = num[i];//round the first number to the smallest, followed by the ratio for (int j = i + 1; j <= Num.length-1; j + +) {if (min > Num[j] ) {min = num[j];}} Determine where the smallest number appears in the position int j;for (j = i; J <= Num.length-1; j + +) {if (num[j] = min) {break;}} J position is minimum//Exchange position if (J > i) {int temp = Num[j];num[j] = num[i];num[i] = temp;}} Print for (int i = 0; I <= num.length-1; i++) {System.out.println (num[i]);}}}


Three: Bubble sort

/* * Bubble sort */public class sort04 {static void sort () {int[] num = {2, 1, 3, 5, 4, 7, 2};for (int i = 0; i < num.length -1; i++) {//comparison n-1 round for (int j = 0; J < num.length-i-1; j + +) {if (Num[j] > num[j + 1]) {int temp = Num[j];num[j] = n Um[j + 1];num[j + 1] = temp;}}} Print for (int i = 0; I <= num.length-1; i++) {System.out.println (num[i]);}} public static void Main (string[] args) {sort ();}}


Four: Hanoi algorithm

/* * Hannotta Hanota: Hanoi (also known as Hanoi) is a puzzle toy derived from an ancient Indian legend. * When great Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. * Big Brahma commands the Brahman to rearrange the discs from below to be placed on another pillar in order of size. * Also stipulates that the disk can not be enlarged on the small disc, between the three pillars can only move one disk at a time * a  column B column C column  *  64 plates, from small to large stacked on a column, now, the request to move the 64 plates, from a column to C column  *  Requirements: In the process of movement, the large plate can not cover the small plate, the use of B-pillar.  *  1 plates: from A to C  *  2 plates, move the small plate to the B-pillar temporary storage, the following large plate moved to the C-pillar, and then the B-pillar on the temporary storage of small plates, moved to the C-pillar.  *  3 plates: What about more plates?  *  N Plates  *  A. Move the top n-1 plate from A, C, to b  *  B. Move the following plate from a to C  *  C. Move the n-1 plate from B to the C 2 64-1 times  */public class sort02 {static void Hanoi (int n, string src, string mid, String des T) {if (n = = 1) {System.out.println (src +--+ + dest),} else {hanoi (n-1, SRC, dest, mid); Hanoi (1, SRC, null, dest); Hanoi (N-1, Mid, SRC, dest);}} public static void Main (string[] args) {Hanoi (3, "A", "B", "C");}}


Java Algorithm Analysis 2 ————— several sort & Hanoi algorithms

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.