All sorting and searching algorithms

Source: Internet
Author: User

All sorting and searching algorithms

Import javax. mail. part;/*** sequential search and sorting ** @ author was built * @ version Creation Time: 04:15:10 */public class SequentialSearch {public static void main (String [] args) {Integer [] a = }; // binary search for non-recursive System. out. println ("binary search non-Recursive Implementation location:" + Binary_Seach (a, 0); // Binary Search recursion implementation System. out. println ("Binary search recursion:" + Binary (a, 0,. length, 1); // interpolation query System. out. println ("interpolation location:" + Insert_Search (a, 3); // Fibonacci lookup // dense Cable Reference (home records are recorded in the notebook) // multipart index (library book) // inverted Index Integer [] a_sort = }; // Shell_Sort (a_sort); // bubble sort Bubble_Sort (a_sort); // insert sort Insert_Sort (a_sort); // select simple sort Select_Sort (a_sort ); // Quick Sort Quick_Sort (a_sort); // Heap_Sort (a_sort); // merge sort Merge_Sort (a_sort); for (int I = 0; I <. length; I ++) {if (a [I] = 8) {System. out. println ("found") ;}} int I = 0; if (a [0] = 1) {System. out. println ("found"); // return 0;} a [0] = 1; while (a [I]! = 1) {I ++;} // return I;} // heap sorting public static void Heap_Sort (Integer [] array) {// filter the array, build a large top heap double len = array. length-1; for (int I = (int) Math. floor (len/2); I> 0; I --) {heapAdjust (array, I, array. length-1);} for (int I = array. length-1; I> 0; I --) {// swap the heap element with the last element, that is, the output maximum value // swap (array, 1, I ); int temp; temp = array [1]; array [1] = array [I]; array [I] = temp; // get the last bit, and change the maximum subscript of the array to the I-1. Adjust from top to leaves to form a new heap. This process is called filtering heapAdjust (array, 1, I-1);} System. err. println (); System. err. println ("heap sorting:"); for (int I = 1; I <array. length; I ++) {System. err. print (array [I] + "") ;}// creates a heap function. In this case, only the keyword s // in [s, m] does not meet the definition of the Big Top heap, by adjusting the [s, m] become a big top heap ================================================== ======================= public static void heapAdjust (Integer [] array, int s, int m) {// use the 0 subscript element as the temporary storage unit array [0] = array [s]; // along the child Large nodes are filtered down for (int j = 2 * s; j <= m; j * = 2) {// ensure that j is the subscript of a large child node, j <m guarantees that j + 1 <= m and does not cross-border if (j <m & array [j] <array [j + 1]) {j ++ ;} if (! (Array [0] <array [j]) {break;} // If the S bit is small, move the older child up to array [s] = array [j]; // if the value of a large child is smaller than the value of S, the heap may be unbalanced. Therefore, the heap where the child is located is filtered by s = j ;} // If the S bit is large, the value remains unchanged; otherwise, the S bit moves down to 2 * s, 4 * s ,... Array [s] = array [0];} // exchange function ==================================================== ======================= public static void swap (Integer [] array, int I, int j) {int temp; temp = array [I]; array [I] = array [j]; array [j] = temp ;} // merge and sort public static void Merge_Sort (Integer [] a) {if (. length> 0) {M_Sort (a, a, 0,. length-1);} System. err. println (); System. err. println ("merge order:"); for (int I = 0; I <. length; I ++) {System. err. p Rint (a [I] + "") ;}// public static void M_Sort (Integer [] a, Integer [] td, int low, int high) {// split int mid; Integer [] td2 = new Integer [. length]; if (low = high) {td [low] = a [high];} else {mid = (low + high)/2; M_Sort (a, td2, low, mid); M_Sort (a, td2, mid + 1, high); Merge (a, td, low, mid, high );}} // Merge sort and Merge public static void Merge (Integer [] a, Integer [] td, int low, int mid, int high) {int m, n, k; For (m = low, n = mid, k = low; m <mid & n 
  
0) {Sort (a, 0,. length-1);} System. err. println (); System. err. println ("Quick Sort:"); for (int I = 0; I <. length; I ++) {System. err. print (a [I] + "") ;}// implement public static void Sort (Integer [] a, int low, int high) {if (low
  
   
A [I]) {I ++;} a [j] = a [I]; // records larger than the central axis are moved to the high end} // a [low] = po; // return I to the end of the central axis record;} // simple selection and sorting public static void Select_Sort (Integer [] a) {for (int I = 0; I <. length; I ++) {int min = I; for (int j = I + 1; j <. length; j ++) {if (a [min]> a [j]) {min = j ;}} if (min! = I) {int temp = a [min]; a [min] = a [I]; a [I] = temp ;}} System. err. println (); System. err. println ("simple choice:"); for (int I = 0; I <. length; I ++) {System. err. print (a [I] + "") ;}// Insert the sorted public static void Insert_Sort (Integer [] a) {for (int I = 1; I <. length; I ++) {if (a [I] = 0; j --) {if (a [I] 1); System. err. println (); System. err. println ("Hill:"); for (int I = 0; I <. length; I ++) {System. err. print (a [I] + "") ;}// bubble sort public static void Bubble_Sort (Integer [] a) {for (int I = 1; I <. length; I ++) {for (int j =. length-1; j> = I; j --) {if (a [j] k) {high = mid-1 ;}else {low = mid + 1 ;}} return-1;} // recursively implement Binary search public static int Binary (Integer [] a, int low, int high, int k) {int mid = (low + high) /2; if (a [mid] = k) {return mid;} else if (a [mid]> k) {return Binary (a, low, mid-1, k);} else {return Binary (a, mid + 1, high, k);} // non-recursive Binary Search public static int Binary_Seach (Integer [], int k) {int low = 0, high =. length, mid =-1; while (low <= high) {mid = (low + high)/2; if (a [mid] = k) {return mid ;} else if (a [mid]> k) {high = mid-1 ;}else {low = mid + 1 ;}} return-1 ;}}
  
 


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.