Java implements various sorting algorithms

Source: Internet
Author: User

Java implements various sorting algorithms
Insert sort

public static void insertSort(int[] arr){        for(int i = 0 ; i < arr.length ; i ++){            int tmp = arr[i];            int j = 0;            for(j = i - 1; (j > -1) && (tmp < arr[j]) ; j --){                arr[j + 1] = arr[j];            }            arr[j + 1] = tmp;        }
Bubble Sorting
Public static void bubbleSort (int [] arr) {int length = arr. length-1; int tmp = 0; boolean flag = false; while (length! = 0) {flag = false; for (int I = 0; I <length; I ++) {if (arr [I]> arr [I + 1]) {tmp = arr [I]; arr [I] = arr [I + 1]; arr [I + 1] = tmp; flag = true ;}} System. out. println ("Number" + (arr. length-length) + ":" + Arrays. toString (arr); length --; if (flag = false) {// No data exchange occurs during this process, note that the array is already ordered with break ;}}}
Select sort
(1): the sequence is divided into ordered and unordered zones. The ordered zone is empty at the beginning, and the disordered zone includes all elements (2 ): each time the smallest element is selected from the unordered area, it is exchanged with the first element in the unordered area (3): Repeat (2) until there is no element in the unordered area.
Public static void selectSort (int [] arr) {int tmp = 0; int index = 0; for (int I = 0; I <arr. length; I ++) {index = I; // find [I... the minimum value in length] for (int j = I + 1; j <arr. length; j ++) {if (arr [index]> arr [j]) {index = j ;}// the minimum element is not its own if (index! = I) {tmp = arr [index]; arr [index] = arr [I]; arr [I] = tmp ;}}}

# Merge Sorting

Public static void mergeSort (int [] list) {if (list. length> 1) {// merge the left half of int [] leftHalf = new int [list. length/2]; System. arraycopy (list, 0, leftHalf, 0, list. length/2); mergeSort (leftHalf); // merge the right half of the int rightHalfLength = list. length-list. length/2; int [] rightHalf = new int [rightHalfLength]; System. arraycopy (list, list. length/2, rightHalf, 0, rightHalfLength); mergeSort (rightHalf); int [] temp = merge (leftHalf, rightHalf); System. arraycopy (temp, 0, list, 0, temp. length) ;}} public static int [] merge (int [] list1, int [] list2) {int [] temp = new int [list1.length + list2.length]; int pos1 = 0; // list1 cursor int pos2 = 0; // list2 cursor int pos3 = 0; // temp cursor while (pos1 <list1.length & pos2 <list2.length) {if (list1 [pos1] <list2 [pos2]) {temp [pos3 ++] = list1 [pos1 ++];} else if (list1 [pos1]> list2 [pos2]) {temp [pos3 ++] = list2 [pos2 ++];} else {temp [pos3 ++] = list2 [pos2 ++]; temp [pos3 ++] = list1 [pos1 ++] ;}} while (pos1 <list1.length) {temp [pos3 ++] = list1 [pos1 ++];} while (pos2 <list2.length) {temp [pos3 ++] = list2 [pos2 ++];} return temp ;}
Quick sorting
Public static void quickSort (int [] arr, int low, int high) {if (high> low) {int part = partition (arr, low, high); // System. out. println ("part =" + part); quickSort (arr, low, part-1); quickSort (arr, part + 1, high );}} // search for the demarcation point public static int partition (int [] arr, int low, int high) {int first = low; int lower = arr [low]; low ++; while (high> low) {// find a number larger than lower on the left while (low <= high & arr [low] <= lower) {low ++ ;} // find the number while (high >=low & arr [high]> lower) {high --;} smaller than or equal to limit on the right --;} // switch if (high> low) {int temp = arr [high]; arr [high] = arr [low]; arr [low] = temp ;}} // at this time, high = low-1; // and the elements before high are smaller than or equal to low, and those after low are greater than low // System. out. println ("low =" + low + "; high =" + high + ":" + Arrays. toString (arr); if (high> first) {int temp = arr [high]; arr [high] = arr [first]; arr [first] = temp; // System. out. println ("part =" + high + ":" + Arrays. toString (arr); return high;} else {// System. out. println ("part =" + high + ":" + Arrays. toString (arr); return first ;}}
Heap sorting
Heap is a binary tree with the following attributes. 1. A full binary tree. 2. Each node is larger than or equal to any of its children.
Public static void heapSort (int arr []) {// initialize heap initHeap (arr); // System. out. println ("initialization heap:" + Arrays. toString (arr); // end indicates the end position of the heap. int end = arr. length-1; while (end> 0) {// Delete the heap top element int temp = arr [0]; arr [0] = arr [end]; arr [end] = temp; end --; if (end> 0) {// adjust the heap adjustHeap (arr, end );}}} public static void initHeap (int [] arr) {// count from 0, the left child is located at location 2i + 1, the right child is at location 2i + 2, while the parent node is at (I-1)/2 for (int I = 1; I <Arr. length; I ++) {int current = I; while (current> 0) {int parent = (current-1)/2; // The child node is greater than the parent node, if (arr [current]> arr [parent]) {int temp = arr [current]; arr [current] = arr [parent]; arr [parent] = temp; // upward adjustment, with the new current = parent;} else {break ;}}} public static void adjustHeap (int [] arr, int end) {// After deleting the heap top element, adjust the position of the heap top. // You Need To Know that deleting the heap top only changes the element value of the heap top, // Therefore, after the heap top is removed, the left and right trees are consistent with the heap top. // this way, the heap top elements are lowered. When the heap top element is detected to comply with the heap definition, // The left and right subtree always comply with the heap definition, so the adjustment process can end int current = 0; while (current <end) {int left = current * 2 + 1; int right = current * 2 + 2; if (left <= end & right <= end) {// both left and right nodes have if (! (Arr [current]> = arr [left] & arr [current]> = arr [right]) {// you need to adjust the heap top int index = left; if (arr [left] <arr [right]) {// The left node is smaller than the right node index = right;} int temp = arr [current]; arr [current] = arr [index]; arr [index] = temp; current = index;} else {// This is a heap. You do not need to adjust break further ;}} else if (left <= end & right> end) {// The left node exists, but the right node does not exist if (arr [current] <arr [left]) {int temp = arr [current]; arr [current] = arr [left]; arr [left] = temp; current = left ;} else {// now is a heap. You do not need to adjust the break downward.} else {// The left and right nodes do not exist. The break has been adjusted to the end ;}}}

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.