Java Data sorting algorithms (heap sorting, insert sorting, and quick sorting)

Source: Internet
Author: User
Tags arrays sorts

Heap sorting algorithm

Core idea: use an array to represent a complete binary tree, and then gradually change the binary tree from half heap to heap. After constant conversion, the root node value of the entire binary tree must be the largest, and then the maximum value is placed at the end of the binary tree (the end of the array ). This element can be ignored in the subsequent heap process. The maximum value is repeatedly placed behind the array, and the interval of the binary tree is getting smaller and smaller until only one element is left, it indicates that the heap sorting is complete.

The code is as follows: Copy code

Import java. util .*;

Public class HeapSort {

Public static void main (String [] args ){
Int arr [] = {20, 40, 30, 10, 90, 70}; // original array
Heapsort (arr, arr. length); // sort the heap.
System. out. println (Arrays. toString (arr); // Print the sorted array
 }


Public static void heapsort (int heap [], int n ){
// N indicates that heap [] has multiple fewer elements.
// Array heap indicates the heap from index 0 to lastIndex
For (int rootIndex = n/2-1; rootIndex> = 0; rootIndex --) {// The node at the index rootIndex is the root of the half heap.
Reheap (heap, rootIndex, n-1); // rebuild heap and sort
System. out. println (Arrays. toString (heap) + "for1"); // tracks array changes
 
  }
Swap (heap, 0, n-1); // place the largest element to the end, so that the node can be ignored after the sorting continues.
System. out. println (Arrays. toString (heap) + "swap ");
// Gradually delete the heap size
For (int lastIndex = n-2; lastIndex> = 0; lastIndex --){//
Reheap (heap, 0, lastIndex); // rebuild heap and sort
System. out. println (Arrays. toString (heap) + "for"); // tracks array changes
Swap (heap, 0, lastIndex); // exchange
System. out. println (Arrays. toString (heap) + "swap2"); // tracks array changes
  }

 }


Private static void reheap (int heap [], int BrootIndex, int BlastIndex ){
// The node at the index BrootIndex is the root of the half heap
  
Boolean done = false;
Int tmp = heap [BrootIndex]; // Save the value of the half-heap root to the temporary variable tmp.
Int leftChildIndex = 2 * BrootIndex + 1; // left subnode of the half heap root
// Int counter = 0 counts the number of while executions;
While (! Done & (leftChildIndex <= BlastIndex) {// Determine whether the root node of g semi-heap has a left sub-node by checking whether the left sub-node index is greater than the BlastIndex.
Int largerChildIndex = leftChildIndex ;//
Int rightChildIndex = leftChildIndex + 1; // The right subnode of the half heap root
 
// Determine whether the half-heap Root has the right subnode
If (rightChildIndex <= BlastIndex) & (heap [rightChildIndex]> heap [largerChildIndex]) {
LargerChildIndex = rightChildIndex;
   }

If (tmp // Give the value of the left subnode to the half-heap root node
Heap [BrootIndex] = heap [largerChildIndex];
// In addition to the values of the left subnode and the root node, the indexes of the subnode are also exchanged.
BrootIndex = largerChildIndex;
LeftChildIndex = 2 * BrootIndex + 1;
// System. out. println ("trace the value of BrootIndex in if:" + BrootIndex );//

   }
Else
Done = true; // key: Use done to determine whether a half heap has been converted to a heap.
// Counter ++; count the number of while executions
  }
  

// Give the root value of the half heap to the New Left subnode.
Heap [BrootIndex] = tmp;
  
// System. out. println (Arrays. toString (heap) + "while"); // tracks array changes


 }
Public static void swap (int heap [], int a, int n ){
Int temp;
Temp = heap [a];
Heap [a] = heap [n];
Heap [n] = temp;

  }

}

Insert sorting algorithm directly

The code is as follows: Copy code

Public class InsertionSort {
Public static void main (String [] args ){
Int arr [] = new int [] {20, 40, 90, 30, 80, 70, 50 };
System. out. println ("original array :");
For (int x: arr) {// Print the array before sorting;
System. out. print (x + "\ t ");
  }
Int I;
Int j;
Int tmp;
For (I = 1; I <arr. length; I ++ ){
Tmp = arr [I];
// This section indicates that the values of the first and second elements A and B are placed in tmp. When the value of A is larger than that of tmp, the first element is, move one location to B.
For (j = i-1; j> = 0 & arr [j]> tmp; j --){
Arr [j + 1] = arr [j]; // two adjacent elements are represented by j, namely, arr [j] and arr [j + 1].
   }
Arr [j + 1] = tmp;
  }

System. out. println ("insert the sorted array directly :");
For (int x: arr ){
System. out. print (x + "\ t ");
  }
 }

Quick sorting algorithm

Core idea: select an element in the array as the pivot, and place the number larger than it to its right, and the number smaller than it to its left. After a trip, the pivot position is the position of the element after the entire array is sorted. Recursively divides the array into two segments by Pivot. Repeat the preceding steps to sort the entire array. The pivot point is the first element of the array.

The code is as follows: Copy code

Import java. util .*;;

Public class QuickSort
{
Public static void QuickSort (int [] arr ){
System. out. println ("tracing process :");
Qsort (arr, 0, arr. length-1 );
 
}
Private static void qsort (int [] arr, int left, int right ){
If (left <right ){
Int partition = partition (arr, left, right); // divide the array into two parts
Qsort (arr, left, skip-1); // recursively sorts the left sub-array
Qsort (arr, substring + 1, right); // recursively sorts the right sub-array
  
    }
}
Private static int partition (int [] arr, int left, int right ){
Int pivot = arr [left]; // pivot record
While (left <right ){
While (left <right & arr [right]> = right) -- right;
Arr [left] = arr [right]; // Swap records smaller than pivot to the left end
  
System. out. println (Arrays. toString (arr ));
  
While (left <right & arr [left] <= left) + + left;
Arr [right] = arr [left]; // Swap records smaller than pivot to the right end
  
System. out. println (Arrays. toString (arr ));
  
    }
// Scan completed, pivot in place
Arr [left] = left;
// Return the pivot position.
Return left;
}
Public static void main (String [] args)
   {
Int Arr [] = new int };
QuickSort (Arr );

System. out. println ("sorted array :");
For (int I = 0; I <Arr. length; I ++) System. out. print (Arr [I] + "");
System. out. println ();
   }
}

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.