Array advanced Apps-sort and find

Source: Internet
Author: User

1. Array sorting

1) Bubble sort

/*** The basic concept of bubble sort is: * Compare adjacent two numbers in turn, place decimals in front, and large numbers on the back. * That is, in the first trip: first compare the 1th and 2nd numbers, put the decimal place before the large number. * Then compare the 2nd and 3rd numbers, put the decimal before the large number, so continue, * until you compare the last two digits, put the decimal before the large number. By the end of the first trip, the largest number was put at the end. On the second trip: still comparing from the first logarithm * (because the 1th number is no longer less than the 2nd number due to the 2nd and 3rd number of interchanges), * Place the decimal before, after the large number, and compare it to the second last number (which is already the largest in the penultimate position), * The second trip ends, Get a new maximum number in the penultimate position * (actually the second largest number in the whole sequence). So go on, repeat the above process until the final sort is finished. */classBubblesort { Public Static voidBubblesort (int[] arr) {         for(inti = 0; i < arr.length-1; i++){             for(intj = 0; J < Arr.length-1-i; J + +){                if(Arr[j] > arr[j+1]) {Sorttest.swap (arr,j,j+1); }            }        }    }}

2) Select sort

/*** * Select the basic idea of sorting: * Compare the first element in turn and all the elements behind it. * At the end of the first time, there will be a minimum value appearing at the front. * In turn*/  Public classSelectsort { Public Static voidSelectsort (int[] arr) {         for(inti = 0; i < arr.length-1; i++) {            intMinflag =i;  for(intj = i + 1; J < Arr.length; J + +) {                if(Arr[minflag] >Arr[j]) {Minflag=J; }            }            if(I! =Minflag)            {Sorttest.swap (arr, I, minflag); }        }    }}

3) Insert Sort

/*** Insert Sort basic idea * divides the sequence of n elements into two parts that are ordered and unordered, as shown in the Insert Sort procedure example: * {{A1},{a2,a3,a4,...,an}} * {{A1⑴,a2⑴},{a3⑴,a4⑴...,an⑴}}} {{ A1 (N-1), A2 (n-1), ...},{an (n-1)}} * Each process is to compare the first element of an unordered sequence with the elements of an ordered sequence of numbers, to find the insertion position and insert the element into the appropriate position of the ordered sequence. */ Public classInsertsort { Public Static voidInsertsort (int[] arr) {         for(inti = 1; i < arr.length; i++) {             for(intj = i-1; J >= 0; j--) {                if(Arr[j] > arr[j+1]) {Sorttest.swap (arr,j,j+1); }            }        }    }}

4) Hill Sort

/*** Hill Sort: First take an integer less than n D1 as the first increment, * divide all records of a file into groups (n divided by D1). All records with a multiple of D1 are placed in the same group. * Direct insert sorting is performed in each group first, then the second increment d2<d1 repeats the above grouping and sorting, * until the increment dt=1 (DT<DT-L<...<D2<D1) is taken, that is, all records are placed in the same group for direct insert sorting. *//*** belongs to the insertion class ordering, is the entire sequence is divided into a number of small sub-sequences to be inserted sort * Sorting process: First take a positive integer d1<n, all the sequence number is separated by D1 array elements in a group, * the group is directly inserted in the sorting; then take D2<D1, Repeat the grouping and sorting operations above; until Di=1, that is, all records are placed in a group until they are sorted * initial: d=5 | |--------------| *--------------| * |--------------| * |--------------| * online |--------------| * The results of a trip * * * * * d=3-------|--------|---------|--------|------ --| *---------|--------| * |----------|--------| * Two results for 49* (d=1) * * |---|---|---|---|---|---|---* * *    |---|---| Three results * The result of a trip to Geneva*/ Public classShellsort { Public Static voidShellsort (int[] arr) {         for(inti = ARR.LENGTH/2; i > 2; i = I/2) {             for(intj = 0; J < I; J + +) {Insertsort (arr, J, I); }} insertsort (arr,0, 1); }     Public Static voidInsertsort (int[] arr,intStartintInc) {         for(inti = Start+inc; I < arr.length;i+=Inc) {             for(intj = i-inc; J >= 0; j-=Inc) {                if(Arr[j] > arr[j+Inc]) {Sorttest.swap (arr,j,j+Inc); }            }        }    }}

5) Quick Sort

/*** Quick Sorting: * A quick sort of algorithm is: * 1) Set two variables I, J, the beginning of the order: i=0,j=n-1; * 2) with the first array element as the key data, assigned to key, that is key=a[0]; * 3) Forward search from J, that is, from the start of the forward search ( J=j-1 is j--), * Find the first value less than key a[j],a[i] and a[j] Exchange; * 4) Search backwards from I, i.e. backward search from before (i=i+1 i.e. i++), * Find the first one greater than key a[i],a[i] and a[j] Exchange; * 5) Repeat 3rd , 4, 5 steps until i=j; * (3,4 step is not found in the program when J=j-1,i=i+1, until found. * When I find and exchange I, the position of the J pointer does not change. * Also when i=j this process must be exactly the end of the final loop of i+ or J completion. )*/ Public classQuickSort { Public Static voidQuincksort (int[] arr) {sort (arr,0, Arr.length-1); }     Public Static voidSortint[] arr,intIintj) {intPivotindex =partition (ARR,I,J); if(I <j) {Sort (Arr,i,pivotindex-1); Sort (Arr,pivotindex+1, J); }    }     Public Static intPartitionint[] Array,intLowintHigh ) {        intPivokey =Array[low];  while(Low <High ) {             while(Low < High && Array[high] >=Pivokey) { High--; } Array[low]=Array[high];  while(Low < High && Array[low] <=Pivokey) { Low++; } Array[high]=Array[low]; } Array[low]=Pivokey; returnLow ; }}

6) Merge sort

/*** * Merge operation (merge), also called merge algorithm, refers to the operation of merging two sorted sequences into a sequence. * If there is a sequence {6,202,100,301,38,8,1} * initial state: [6] [202] [100] [301] [38] [8] [1] comparison * I=1 [6 202] [100 301] [8 38] [1 ] 3 * i=2 [6 202 301] [1 8] 4 * i=3 [1 6 8 [202 301] 4*/ Public classMergeSort { Public Static voidMergeSort (int[] arr) {sort (arr,0,arr.length-1); }     Public Static voidSortint[] arr,intLowintHigh ) {        intMid = (low + high)/2; if(Low <High ) {            //leftsort (arr, low, mid); //RightSort (arr, Mid + 1, high); //merge right and leftmerge (arr, Low, Mid, high); }    }     Public Static voidMergeint[] arr,intLowintMidintHigh ) {        int[] temp =New int[High-Low + 1]; inti = low;//left Pointer        intj = mid + 1;//Right Pointer        intK = 0; //move the smaller number first to the new array         while(I <= mid && J <=High ) {            if(Arr[i] <Arr[j]) {Temp[k+ +] = arr[i++]; } Else{temp[k+ +] = arr[j++]; }        }        //Move the left remaining number into the array         while(I <=mid) {temp[k+ +] = arr[i++]; }        //move the remaining number in the right side into the array         while(J <=High ) {Temp[k+ +] = arr[j++]; }        //overwrites the number in the new array with the Nums array         for(intK2 = 0; K2 < Temp.length; k2++) {Arr[k2+ Low] =TEMP[K2]; }    }}

7) Heap Sorting

 Public classHeapsort { Public Static voidHeapsort (int[] arr) {buildheap (arr);//Build Heap        inti = 0;  for(i = arr.length-1; I >= 1; i--) {Sorttest.swap (arr),0, i); Heapify (arr,0, i); }    }     Public Static voidBuildheap (int[] arr) {        intn =arr.length;  for(inti = N/2-1; I >= 0; i--) {heapify (arr, I, N); }    }     Public Static voidHeapify (int[] arr,intIdxintmax) {        intleft = 2 * idx + 1; intrigth = 2 * idx + 2; intLargest = 0; if(Left < Max && Arr[left] >Arr[idx]) largest=Left ; Elselargest=idx; if(Rigth < Max && Arr[rigth] >Arr[largest]) largest=rigth; if(Largest! =idx) {sorttest.swap (arr, largest, IDX);        Heapify (arr, largest, max); }    }}

2, array two-point search

 Public classBinarySearch { Public Static voidMain (string[] args) {int[] arr = {11, 22, 33, 44, 55, 66, 77, 88, 99}; intindex = BinarySearch (arr, 88); System.out.println ("Index:" +index); Index= BinarySearch (arr, 100); System.out.println ("Index:" +index); }     Public Static intBinarySearch (int[] arr,intvalue) {        intLow = 0; intHigh = Arr.length-1; intMID =-1;  while(Low <High ) {Mid= (low + high)/2; if(Arr[mid] = =value) {                returnmid; }            if(Value >Arr[mid]) Low= Mid + 1; Else High= Mid-1; }        return-1; }}

Array advanced Apps-sort and find

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.