Java array Sorting, two-point lookup

Source: Internet
Author: User

public static void Main (string[] args) {

Int[] A = {5, 1, 10, 3, 8, 0};

for (int i = 0; i < Selectionsort (a). Length; i++) {

System.out.println (Selectionsort (a) [i]);

}

System.out.println ("-------------------------");

for (int i = 0; i < Bubblesort (a). Length; i++) {

System.out.println (Bubblesort (a) [i]);

}

System.out.println ("-------------------------");

Int[] B = {1, 6, 7, 8};

System.out.println (Bianrysearch (b, 6));


}


Select Sort (Ascending)

private static int[] Selectionsort (int[] arr) {

if (arr! = null) {

for (int i = 0; i < arr.length-1; i++) {

for (int j = i + 1; j < Arr.length; J + +) {

if (Arr[i] > Arr[j]) {

int temp = Arr[i];

Arr[i] = Arr[j];

ARR[J] = temp;

}

}

}

}

return arr;

}


Bubble sort

private static int[] Bubblesort (int[] arr) {

if (arr! = null) {

for (int i = 0; i < arr.length-1; i++) {

for (int j = 0; J < arr.length-i-1; j + +) {

if (Arr[j] < Arr[j + 1]) {

int temp = arr[j + 1];

Arr[j + 1] = Arr[j];

ARR[J] = temp;

}

}

}

}

return arr;

}


Binary lookup (the array must be ordered)

private static int Bianrysearch (int[] arr, int key) {

Starting Angle Label

int a = 0;

End Corner Mark

int b = arr.length-1;

int m = 0;

while (a <= b) {

int midx = (A + b)/2;

m = Arr[midx];

if (key > m) {

A = Midx + 1;

} else if (key < m) {

b = midx-1;

} else {

return MIDX;

}

}

return-1;

}


Java array Sorting, two-point lookup

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.