Review of common algorithms

Source: Internet
Author: User
Tags pow sorts

Package SUANFA;

Import Java.lang.reflect.Array;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;
Import java.util.List;

public class Maopao {
public static void Main (string[] args) {
int a[] = {6, 5, 2, 29,9,567};
Charu (a);
Guibinpaixu (A, 0, 2, new int [3]);
Kuaisupaixu (A, 0, 2);
Sort (a);
System.out.print (A[0]);
System.out.print (a[1]);
System.out.print (a[2]);
System.out.print (A[3]);
System.out.print (A[4]);
System.out.print (A[5]);
}

The cardinality sort will be the total number of digits, then sorted by single digit, 10 bits, etc., and placed in the corresponding position (array plus list); array order after bits
public static void sort (int[] array) {
First, the number of sorts is determined;
int max = array[0];
for (int i = 1; i < Array.Length; i++) {
if (Array[i] > max) {
max = Array[i];
}
}
int time = 0;
Determine the number of digits;
while (Max > 0) {
Max/= 10;
time++;
}
Set up 10 queues;
list<arraylist> queue = new arraylist<arraylist> ();
for (int i = 0; i < i++) {
arraylist<integer> queue1 = new arraylist<integer> ();
Queue.add (queue1);
}
Time allocation and collection;
for (int i = 0; i < time; i++) {
assigning array elements;
for (int j = 0; J < Array.Length; J + +) {
Get the number of time+1 digits;
int x = array[j]% (int) Math.pow (ten, i + 1)/(int) Math.pow (ten, I);
arraylist<integer> queue2 = queue.get (x);
Queue2.add (Array[j]);
Queue.set (x, queue2);
}
int count = 0;//element counter;
Collect queue elements;
for (int k = 0; k < k++) {
while (Queue.get (k). Size () > 0) {
arraylist<integer> Queue3 = Queue.get (k);
Array[count] = queue3.get (0);
Queue3.remove (0);
count++;
}
}
}
}
Bubble sort starts with the first number, 22 compares and exchanges the position, selects the maximum number for the first time, the second time selects two large number; cycle (n-1) * (n-2) ... The array is ordered after the second
public static int[] Maopaopaixu (int[] array) {
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; J < array.length-1-I; j + +) {
if (Array[j] > array[j + 1]) {
int temp = array[j + 1];
Array[j + 1] = Array[j];
ARRAY[J] = temp;
}
}
}
return null;
}
Merge sort divides the array into two arrays, divides two arrays into four arrays, until a single number, 22 sorts
public static int[] Guibinpaixu (int[] array, int first, int. last, int temp[]) {
if (first < last) {
int mid = (first + last)/2;
Guibinpaixu (Array, first, mid, temp);
Guibinpaixu (Array, mid + 1, last, temp);
Guibinsort (Array, first, mid, last, temp);
}
return null;
}
Merge sort to sort two ordered arrays
public static void Guibinsort (int[] Array, int. first, int mid, int last, int temp[]) {
int i = First, j = mid + 1;
int m = Mid, n = last;
int k = 0;
while (I <= m && J <= N) {
if (Array[i] <= array[j]) {
temp[k++] = array[i++];
} else {
temp[k++] = array[j++];
}
}
while (I <= m) {
temp[k++] = array[i++];
}
while (j <= N) {
temp[k++] = array[j++];
}
for (int k2 = 0; K2 < K; k2++) {
Array[first + K2] = Temp[k2];
}
}
Quick Sort Select a cardinality that will be greater than its rank on the left, less than it's ranked right, recursively, to get an ordered array
public static int[] Kuaisupaixu (int[] Array, int. low, int hight) {
if (Low < hight) {
int moddle = Getmiddle (array, low, hight);
Kuaisupaixu (array, low, moddle-1);
Kuaisupaixu (Array, Moddle + 1, hight);
}
return null;
}
Quick sort selection datum; sort the array by datum
public static int Getmiddle (int[] Array, int. low, int hight) {
int temp = Array[low];
while (Low < hight) {
while (Low < hight && Array[hight] >= temp) {
hight--;
}
Array[low] = Array[hight];
while (Low < hight && Array[low] < temp) {
low++;
}
Array[hight] = Array[low];
}
Array[low] = temp;
return low;
}
Select Sort to select a minimum number for the datum to place in the first place, the second small place in the second place, sort by sequence
public static int[] Xuanzepaixu (int[] array) {
for (int i = 0; i < Array.Length; i++) {
int k = i;
for (int j = i + 1; j < Array.Length; J + +) {
if (Array[j] < array[k]) {
K = J;
}
}
if (i! = k) {
int temp = Array[i];
Array[i] = array[k];
ARRAY[K] = temp;
}
}
return null;
}
Insert sort the first number is an ordered array, starting with the second number, comparing with the previous array insert sort, knowing the last number
public static int[] Charu (int[] array) {
int temp = 0;
for (int i = 1; i < Array.Length; i++) {
int j = i-1;
temp = Array[i];
for (; J >= 0 && temp < array[j]; j--) {
Array[j + 1] = Array[j]; Move the value greater than temp to one unit after the whole
}
Array[j + 1] = temp;
}
return null;
}

}

Review of common 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.