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