Java bubble Sort and quick Sort code

Source: Internet
Author: User

Bubble Sort:

Basic idea: In order to sort a group of numbers, the current is not yet ranked in the range of all the number, the top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.
public class bubblesorted{
Public bubblesorted () {
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int temp=0;
for (int i=0;i<a.length-1;i++) {
for (int j=0;j<a.length-1-i;j++) {
if (A[j]>a[j+1]) {
TEMP=A[J];
A[J]=A[J+1];
A[j+1]=temp;
}
}
}
for (int i=0;i<a.length;i++)
System.out.println (A[i]);
} }

Quick sort:

Algorithm: It is suitable to use this method when the amount of data is large. When searching by binary method, the data need to be ordered and not duplicated. Basic idea: Assume that the data is sorted in ascending order, for a given value x, from the middle of the sequence of the comparison, if the current position value equals X, then the search succeeds; if x is less than the current position value, the first half of the sequence is searched, and if x is greater than the current position, the second half of the series is searched until

Given an array of {12, 23, 34, 45, 56, 67, 77, 89, 90}, it is now required to use a dichotomy to find the specified value and return it at the index of the array, or 1 if not found. The code is as follows:

Package com.test;

public class findsorted{
public static void Main (string[] args) {
int[] arr = new int[] {12, 23, 34, 45, 56, 67, 77, 89, 90};
System.out.println (Search (arr, 12));
System.out.println (Search (arr, 45));
System.out.println (Search (arr, 67));
System.out.println (Search (arr, 89));
System.out.println (Search (arr, 99));
}

public static int search (int[] arr, int key) {
int start = 0;
int end = Arr.length-1;
while (start <= end) {
int middle = (start + end)/2;
if (Key < Arr[middle]) {
end = Middle-1;
} else if (Key > Arr[middle]) {
Start = middle + 1;
} else {
return middle;
}
}
return-1;
}
}

Java bubble Sort and quick Sort code

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.