Arrays of the first season of Java Introduction (sorting issues)

Source: Internet
Author: User

One: Bubble sort
/* * array Sort by bubble sort: * Adjacent elements 22 comparison, large back, the first time, the maximum appears at the maximum index * * Extension: * Using the bubble sorting method, you can get the maximum value of an array (first bubble sort, take the last value) and the minimum value (call bubble sort, take the first value) */public class Array Demo {public static void main (string[] args) {//define an array int[] arr = {24, 69, 80, 57, 13}; System.out.println ("Pre-sort:");p Rintarray (arr);//Because there may be multiple arrays to sort, write the method Bubblesort (arr); System.out.println ("After sorting:");p Rintarray (arr);} Bubble sort code public static void Bubblesort (int[] arr) {for (int x = 0; x < arr.length-1; + +) {//Outer loop control comparison number of times (arr.length-1) round F or (int y = 0; y < arr.length-1-X; y++) {//arr.length-1 is to prevent data from being out of bounds,//arr.length-1-X is to reduce the number of comparisons if (Arr[y] > Arr[y + 1]) {//The preceding number is greater than the number behind. Interchange int temp = Arr[y];arr[y] = arr[y + 1];arr[y + 1] = temp;}}} Traverse function public static void PrintArray (int[] arr) {System.out.print ("["); for (int x = 0; x < arr.length; X + +) {if (x = = arr.length-1) {System.out.print (arr[x]+ "]"),} else {System.out.print (arr[x] + ",");}}} 


Two: Select sort

/* Array Sort selection Sort: * Starting with 0 index, comparing with subsequent elements, small forward, first completed, minimum value appears at minimum index */public class Arraydemo {public static void main (string[] Arg s) {//define an array int[] arr = {24, 69, 80, 57, 13}; System.out.println ("Pre-order:");p Rintarray (arr);//Improved Selectsort (arr) with methods; System.out.println ("After sorting:");p Rintarray (arr);} public static void Selectsort (int[] arr) {for (int x=0; x<arr.length-1; x + +) {//Total arr.length-1 round for (int y=x+1; y< Arr.length; y++) {if (Arr[y] <arr[x]) {//If the subsequent number is less than the previous number, the interchange always makes the preceding small value int temp = arr[x];arr[x] = Arr[y]; arr[y] = temp;}}} Traverse function public static void PrintArray (int[] arr) {System.out.print ("["); for (int x = 0; x < arr.length; X + +) {if (x = = arr.length-1) {System.out.print (arr[x]),} else {System.out.print (arr[x] + ",");}} System.out.println ("]");}}

Three: two-point search

/* Search: * Basic find: Array elements unordered (find tail from scratch) * Binary lookup (binary lookup): array elements ordered *  * Analysis: * A: Define maximum Index, minimum index * B: Calculate Intermediate Index * C: Compare the value of the intermediate index with the value to find * Equal: Returns the current Intermediate index * Not equal: * Big left to find * small right to find * D: Recalculate Intermediate index * big left to find * max = mid-1; * Small Right to find * min = mid + 1; * E: Back to B */public class Arraydemo {public static void main (string[] args) {//define an array int[] arr = {11,22,33,44,55,66,77};//write function Implement int index = GetIndex (arr, 33); System.out.println ("index:" +index);//What happens if this element does not exist? index = GetIndex (arr, 333); System.out.println ("index:" +index);//-1}public static int GetIndex (int[] arr,int value) {//define maximum index, minimum index int max = Arr.length-1;int min = 0;//calculates the intermediate index int mid = (max +min)/2;//compares the value of the intermediate index with the value to look for while (Arr[mid]! = value) {//while implements the loop if (arr[ Mid]>value) {//Big Max = mid-1;//left looking for}else if (arr[mid]<value) {//small min = mid + 1;//right find}//join to determine if (Min > Max) {//Cannot find retur n-1;} Mid = (max +min)/2;//re-fetch the middle value of the new round array}return mid;//arr[mid] = value}}



Arrays of the first season of Java Introduction (sorting issues)

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.