Various sorting algorithms (Java)

Source: Internet
Author: User

    /*** Time complexity: O (n*n) * Simple selection sorting: Select a minimum value from the remaining elements each time, exchange the minimum value and the value in the corresponding position in the array * N Trip *@paramNums*/     Public Static voidSimpleselectsort (int[] nums) {                 for(inti = 0;i < nums.length;i++) {//Number of Trips            intK = i;//Staging Current Location            intmin = nums[i];//staging the current element//find the smallest value in the remaining elements             for(intj = i + 1; J < nums.length;j++){                if(Nums[j] <min) {min=Nums[j]; K=J; }            }                        //swapping elements, exchanging elements based on the subscript of an element            if(I! =k) {Nums[k]=Nums[i]; Nums[i]=min; }            }    }
    /*** Time complexity: O (n*n) * Direct insertion sorting: one from the remaining elements sequentially, inserted into the preceding ordered subsequence * n-1 *@paramNums*/     Public Static voidInsertsort (int[] nums) {        if(Nums = =NULL)return; intLen =nums.length; inttmp; intJ//Insert Position         for(inti = 1; i < Len; i++) {//n-1 TripTMP = Nums[i];//Temporary storage             for(j = 0; J < i && Nums[i] >= nums[j]; j + +);//Nums[i] > Nums[j] End, find the entry and exit position, J is the insertion position             for(intK = i;k >= j + 1;k--)//ShiftNums[k] = nums[k-1]; NUMS[J]= tmp;//Inserting values        }    }
/*** Bubble Sort time complexity * time complexity: O (n*n) * n-1 trip *@paramNums*/     Public Static voidBubblesort (int[] nums) {        if(Nums = =NULL)return; BooleanFlag =false;//do you have any exchange for a trip ?        intTmp,len =nums.length;  for(inti = 0; i < len-1; i++) {//Number of Trips             for(intj = 0; J < len-1-I; J + +) {//the number of elements to compare per trip                if(Nums[j] > nums[j + 1]) {//Interchange ElementFlag =true; TMP=Nums[j]; NUMS[J]= Nums[j+1]; Nums[j+1] =tmp; }            }            if(!flag)//if, after a trip, there is no element exchange, the end                 Break; }    }
/*** Array Rollover * time complexity: O (n) *@paramNums*/     Public Static voidReverseint[] nums) {                if(Nums = =NULL)return; inttmp; intLen =nums.length; //Flip an array element         for(inti = 0,k = len-1; I <= K; i++,k--) {tmp=Nums[i]; Nums[i]=Nums[k]; NUMS[K]=tmp; }    }
/*** Quick Sort: Recursive operation * Defines the pivot, the element at the pivot divides the array into two parts, which are larger than the right side, and not greater than the left side, * so that the elements as pivots are adjusted to the correct position after sorting. * Recursive fast sequencing of left and right two molecular sequences is carried out respectively.     Until all elements have been adjusted to the sorted position. * Method: Two pointers, two pointers, respectively from left and right to traverse * Time complexity: O (NLOGN), average time complexity performance best *@paramNums*/     Public Static voidQuickSort (int[] Nums,intLeftintright) {//Left : Nether, right: Upper bound                if(Left <Right ) {            intPivot = Nums[left];//Pivot            intLow =Left ; intHigh =Right ;  while(Low < High) {//a quick round of sorting, dividing it into left and right two parts                 while(Low < High && Nums[high] > Pivot)//find the first element that is not more than pivot from right to lefthigh--; Nums[low]= Nums[high];//Moving Elements                 while(Low < High && Nums[low] <= pivot)//find the first element larger than pivot from left to rightlow++; Nums[high]= Nums[low];//Moving Elements} Nums[low]= pivot;//pivot elements placed in the final position after sorting//Quick row of left and right parts respectivelyQuickSort (Nums, left, low-1); QuickSort (Nums, Low+ 1, right); }    }

Various sorting algorithms (Java)

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.