Current location - Loan Platform Complete Network - Big data management - Java big data sorting algorithm
Java big data sorting algorithm
I haven't read the code for a long time. Recently, I planned to review java, and suddenly I thought of sorting algorithm. I used java to knock on several commonly used sorting algorithms, and the unordered sequences were uniformly arranged from small to large.

Selective sorting is a simple and intuitive sorting algorithm. Its working principle is: select the smallest element from the data elements to be sorted for the first time and store it at the beginning of the sequence, then find the smallest element from the remaining unsorted elements and continue to put it in the next position until the number of elements to be sorted is 0.

Select the classification code as follows:

public void Select _ sort(int[]arr){

int temp,index

for(int I = 0; I< 10; i++) {

index = I;

for(int j = I+ 1; j & lt 10 ; j++) {

if(arr[j]& lt; Arr[ index])

index = j;

}

/*

temp = arr[I];

arr[I]= arr[index];

arr[index]= temp;

*/

swap(arr,I,index);

}

System.out.print ("After sorting by selection:");

for(int I = 0; I< 10; i++)

system . out . print(arr[I]+" ");

system . out . println(" ");

}

Bubble sorting is a basic sorting algorithm. Its idea is to compare adjacent elements in pairs, with the larger element in the back and the smaller element in the front, so that the largest element will return to its original place once it is circulated. If the number of elements in the array is n, then after (n- 1) times, all elements will be sorted from small to large. The whole process is like bubble rising, so it is called bubble sequencing.

Select the classification code as follows:

public void Bubble _ sort(int[]arr){

Internal temperature;

for(int I = 0; I<9; i++) {

for(int j = 0; j & lt 10-I- 1; j++) {

if(arr[j]& gt; arr[j+ 1]) {

/*

temp = arr[j];

arr[j]= arr[j+ 1];

arr[j+ 1]= temp;

*/

swap(arr,j,j+ 1);

}

}

}

System.out.print ("after bubble sorting:");

for(int I = 0; I< 10; i++)

system . out . print(arr[I]+" ");

system . out . println(" ");

}

Insertion sorting is also a common sorting algorithm. The idea of insert sorting is to create an array with the same size as the array to be sorted, take out the elements to be sorted in the array one by one, and then insert them into the appropriate position in the new array, so that the elements in the new array are arranged in the order from small to large.

Insert the classification code as follows:

public void Insert _ sort(int[]arr){

Int length = arr.length

int[]arr _ sort = new int[length];

int count = 0;

for(int I = 0; I < length; i++) {

if(count == 0) {

arr _ sort[0]= arr[0];

} else if(arr[I]& gt; = arr_sort[count - 1]) {

arr _ sort[count]= arr[I];

} else if(arr[I]& lt; arr_sort[0]) {

insert(arr,arr_sort,arr[i],0,count);

} Otherwise {

for(int j = 0; j & ltcount- 1; j++) {

if(arr[I]& gt; = arr _ sort[j]& amp; & amparr[I]& lt; arr_sort[j+ 1]) {

insert(arr,arr_sort,arr[i],j+ 1,count);

Break;

}

}

}

count++;

}

System.out.print ("After inserting sort:");

for(int I = 0; I< 10; i++)

system . out . print(arr _ sort[I]+" ");

system . out . println(" ");

}

public void insert(int[] arr,int[] arr_sort,int value,int index,int count) {

for(int I = count; I> index; I-)

arr _ sort[I]= arr _ sort[I- 1];

arr _ sort[index]= value;

}

Compared with bubble sorting algorithm, the efficiency of quick sorting is greatly improved. Because when bubble sorting is used, an outer loop can only return one value. If there are n elements, the outer loop will be executed at most (n- 1) times. When you use quick sorting, you can divide all the elements into two piles at once according to their size, that is, it takes logn rounds on average to complete sorting.

The idea of quick sorting is to select a reference value (the first element here is the reference value) at each sorting, then compare all the elements with the reference value and divide them into two piles according to the size, and then recursively perform the process until all the elements are sorted.

public void Quick_sort(int[] arr,int left,int right) {

if(left & gt; = right)

Return;

int temp,t;

int j = right

int i = left

temp = arr[left];

While (I & ltj) {

While(arr[j] > temperature & me<j)

j-;

while(arr[I]& lt; Temperature & me<j)

i++;

If (I & ltj) {

t = arr[I];

arr[I]= arr[j];

arr[j]= t;

}

}

arr[left]= arr[I];

arr[I]= temp;

Quick_sort(arr,left,I- 1);

Quick_sort(arr, i+1, right);

}

Merge sorting is an effective sorting algorithm based on merge operation. Merge sorting groups the elements of the sequence layer by layer, and then compares and sorts them from the smallest group. Every two small groups merge into a big group, and finally all the elements are in order.

public void Mergesort(int[] arr,int left,int right) {

If (right-left & gt0) {

Int[] arr_ 1 = new int[ (right-left)/2+1];

int[]arr _ 2 = new int[(right-left+ 1)/2];

int j = 0;

int k = 0;

for(int I = left; I<= right; i++) {

If (I < = (right+left) /2) {

arr _ 1[j++]= arr[I];

} Otherwise {

arr _ 2[k++]= arr[I];

}

}

Mergesort(arr_ 1, 0, (right-left)/2);

Mergesort (arr _ 2,0, (right-left-1)/2);

Merge(arr_ 1,arr_2,arr);

}

}

public void Merge(int[]arr _ 1,int[] arr_2,int[] arr) {

int I = 0;

int j = 0;

int k = 0;

int l 1 = arr _ 1 . length;

int L2 = arr _ 2 . length;

While (I<L1&; & ampj & ltL2) {

if(arr _ 1[I]& lt; = arr_2[j]) {

arr[k]= arr _ 1[I];

i++;

} Otherwise {

arr[k]= arr _ 2[j];

j++;

}

k++;

}

if(i == L 1) {

for(int t = j; j & ltL2; j++)

arr[k++]= arr _ 2[j];

} Otherwise {

for(int t = I; I<L1; i++)

arr[k++]= arr _ 1[I];

}

}

For the merging and sorting here, I use variables such as left and right to make it universal, so it is better to use numbers directly to make it clear, so I give relevant pseudo-code for easy understanding.

Mergesort(arr[0...n- 1])

//input: a sortable array arr[0...n- 1]

//output: non-descending array arr[0...n- 1]

If n> 1

Copy the array [0...n/2- 1] to arr _ 1 [0 ... (n+1)/2-1]/Make sure the number of elements in arr _1> = arr.

//When the total number is odd, arr_ 1 has one more element than arr_2; When the total number is even, it has no effect.

Copy the arrangement [n/2...n- 1] to arr_2[0...n/2- 1]

Mergesort(arr_ 1[0...(n+ 1)/2- 1])

Mergesort(arr_2[0...n/2- 1])

Merge(arr_ 1,arr_2,arr)

Merge(arr_ 1[0...p- 1],arr_2[0...q- 1],arr[0...p+q- 1])

//Input: two ordered arrays arr_ 1[0...p- 1] and arr_2[0...q- 1].

//Output: combine the numbers arr_ 1 and arr_2 to form arr.

int i & lt-0; j & lt-0; k & lt-0

when I

& ltp span= ""Do < = " " j if arr _ 1[I]& lt; = arr 2[j]

arr[k]& lt; - arr_ 1[i]

I & lt-i+ 1

else arr[k]& lt; -arr _ 2[j]; j & lt-j+ 1

k & lt-k+ 1

If i=p

Copy the array 2[j...q- 1] to arr[k...p+q- 1].

Otherwise, copy arr_ 1[i...p- 1] to arr[k...p+q- 1]

Package test _1;

Import java.util.scanner;

Public class Test0 1 {

Public static void main(String[] args) {

Scanner sc = new scanner (system. in);

int[]arr _ 1 = new int[ 10];

for(int I = 0; I< 10; i++)

arr _ 1[I]= sc . nextint();

Sort demo _1= newsort ();

// 1~5 can only run one at a time. If multiple items are running at the same time, only the first item is valid, and the rest are invalid sorting. Because the first run has sorted the sorted array.

demo_ 1。 select _ sort(arr _ 1); // - 1

//demo_ 1。 bubble _ sort(arr _ 1); // - 2

/* // - 3

demo_ 1。 Quick_sort(arr_ 1,0,arr _ 1 . length- 1);

System.out.print ("After quick sorting:");

for(int I = 0; I< 10; i++)

system . out . print(arr _ 1[I]+" ");

system . out . println(" ");

*/

//demo_ 1。 insert _ sort(arr _ 1); // - 4

/* // - 5

demo_ 1。 Mergesort(arr_ 1,0,arr _ 1 . length- 1);

System.out.print ("After merging and sorting:");

for(int I = 0; I< 10; i++)

system . out . print(arr _ 1[I]+" ");

system . out . println(" ");

*/

}

}

Category sorting {

public void swap(int arr[],int a,int b) {

int t;

t = arr[a];

arr[a]= arr[b];

arr[b]= t;

}

public void Select _ sort(int[]arr){

int temp,index

for(int I = 0; I< 10; i++) {

index = I;

for(int j = I+ 1; j & lt 10 ; j++) {

if(arr[j]& lt; Arr[ index])

index = j;

}

/*

temp = arr[I];

arr[I]= arr[index];

arr[index]= temp;

*/

swap(arr,I,index);

}

System.out.print ("After sorting by selection:");

for(int I = 0; I< 10; i++)

system . out . print(arr[I]+" ");

system . out . println(" ");

}

public void Bubble _ sort(int[]arr){

Internal temperature;

for(int I = 0; I<9; i++) {

for(int j = 0; j & lt 10-I- 1; j++) {

if(arr[j]& gt; arr[j+ 1]) {

/*

temp = arr[j];

arr[j]= arr[j+ 1];

arr[j+ 1]= temp;

*/

swap(arr,j,j+ 1);

}

}

}

System.out.print ("after bubble sorting:");

for(int I = 0; I< 10; i++)

system . out . print(arr[I]+" ");

system . out . println(" ");

}

public void Quick_sort(int[] arr,int left,int right) {

if(left & gt; = right)

Return;

int temp,t;

int j = right

int i = left

temp = arr[left];

While (I & ltj) {

While(arr[j] > temperature & me<j)

j-;

while(arr[I]& lt; Temperature & me<j)

i++;

If (I & ltj) {

t = arr[I];

arr[I]= arr[j];

arr[j]= t;

}

}

arr[left]= arr[I];

arr[I]= temp;

Quick_sort(arr,left,I- 1);

Quick_sort(arr, i+1, right);

}

public void Insert _ sort(int[]arr){

Int length = arr.length

int[]arr _ sort = new int[length];

int count = 0;

for(int I = 0; I < length; i++) {

if(count == 0) {

arr _ sort[0]= arr[0];

} else if(arr[I]& gt; = arr_sort[count - 1]) {

arr _ sort[count]= arr[I];

} else if(arr[I]& lt; arr_sort[0]) {

insert(arr,arr_sort,arr[i],0,count);

} Otherwise {

for(int j = 0; j & ltcount- 1; j++) {

if(arr[I]& gt; = arr _ sort[j]& amp; & amparr[I]& lt; arr_sort[j+ 1]) {

insert(arr,arr_sort,arr[i],j+ 1,count);

Break;

}

}

}

count++;

}

System.out.print ("After inserting sort:");

for(int I = 0; I< 10; i++)

system . out . print(arr _ sort[I]+" ");

system . out . println(" ");

}

public void insert(int[] arr,int[] arr_sort,int value,int index,int count) {

for(int I = count; I> index; I-)

arr _ sort[I]= arr _ sort[I- 1];

arr _ sort[index]= value;

}

public void Mergesort(int[] arr,int left,int right) {

If (right-left & gt0) {

Int[] arr_ 1 = new int[ (right-left)/2+1];

int[]arr _ 2 = new int[(right-left+ 1)/2];

int j = 0;

int k = 0;

for(int I = left; I<= right; i++) {

If (I < = (right+left) /2) {

arr _ 1[j++]= arr[I];

} Otherwise {

arr _ 2[k++]= arr[I];

}

}

Mergesort(arr_ 1, 0, (right-left)/2);

Mergesort (arr _ 2,0, (right-left-1)/2);

Merge(arr_ 1,arr_2,arr);

}

}

public void Merge(int[]arr _ 1,int[] arr_2,int[] arr) {

int I = 0;

int j = 0;

int k = 0;

int l 1 = arr _ 1 . length;

int L2 = arr _ 2 . length;

While (I<L1&; & ampj & ltL2) {

if(arr _ 1[I]& lt; = arr_2[j]) {

arr[k]= arr _ 1[I];

i++;

} Otherwise {

arr[k]= arr _ 2[j];

j++;

}

k++;

}

if(i == L 1) {

for(int t = j; j & ltL2; j++)

arr[k++]= arr _ 2[j];

} Otherwise {

for(int t = I; I<L1; i++)

arr[k++]= arr _ 1[I];

}

}

}

Please correct me if there are any mistakes. I appreciate it.