10.3lc
Today is another day lying flat.
National Day is really a good day to get married ~ ~ ~ my father's colleagues have a wedding banquet every day
Wrote the daily easy question: and a few simple returns to an eight order:
Personal feeling: in fact, it is mainly inserted into the pile and quickly arranged.
Complexity of fast scheduling: it has a great relationship with partition points. It is recommended to select it along with the calculation
Let's review the ranking
a eight sorts
1. Bubble sorting
Idea: compare in sequence, select the largest one from the back to the front, and know the comparison arr.length-1 times;
public static void bublerSorted(int[] arr){ //Exchange variables during picking; int temp=0; int length=arr.length; for(int i=0;i<leng-1;i++){ //J < leng-1-i: determine the I-bit after each cycle comparison, so it will -i compare the elements of the remaining positions for(int j=0;j<leng-1-i;j++){ if(arr[j]>arr[j+1]){ temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } System.out.println(Arrays.toString(arr)); } }
2. Select Sorting
Idea: choose the smallest one: from to the back row: similar to the small one in the first row and the large one in the back row: the index is stored, so the speed is a little faster than bubbling:
Because in an internal for loop, the array stores only the minimum value and the index of the minimum value, saving one less value is faster
public static void chooseSorted(int[] arr){ for(int i=0;i<arr.length-1;i++){ int minIndex=i; int min=arr[i]; for(int j=i+1;j<arr.length-1,j++){ if(min >arr[j]){ min=arr[j]; minIndex=j; } if(minIndex !=0){ arr[minIndex]=arr[i]; arr[i]=min; } } } System.out.println(Arrays.toString(arr)); }
3. Insert sort:
Idea: each time the last number is inserted, the actual position is i-1; Similar to playing cards!
public static void insertSotred(int[] arr){ int insertVal=0; int inserIndex=0; for(int i=0;i<arr.length-1;i++){ insertVal=arr[i+1]; inserIndex=i; while(insertIndex >= 0 &&arr[insertIndex]> insertVal){ arr[insertIndex+1]=arr[insertIndex]; insertIndex--; } arr[insertIndex+1] = insertVal; } System.out.println(Arrays.toString(arr)); }
public static void insertSotred(int[] arr){ for(int i=0;i<arr.length-1;i++){ for(int j=i-1;j>0&&arr[j]>arr[j+1];j--;) int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } System.out.println(Arrays.toString(arr)); }
4. Hill sort (exchange)
Idea: optimization of insertion sorting: packet switching
public static void shellSorted(int[] arr){ int temp=0; //Temporary intermediate variable for(int gap = arr.length/2;gap >= 0;gap /=2){ //i<arr.lenth; To ensure that the last one can eat and live: for(int i=gap;i<arr.lenth;i++){ for(int j=i-gap; j>=0; j-=gap){ if(arr[j] > arr[j+gap]){ temp=arr[j]; arr[j]=arr[j+gap]; arr[j+gap]=temp; } } } } }
5. Hill sorting (interleaving) fast
Thought: insert the original thought conversion bit based on exchange!
public static void shellSorted(int[] arr){ int temp=0; //Temporary intermediate variable for(int gap = arr.length/2;gap >= 0;gap /=2){ //i<arr.lenth; To ensure that the last one can eat and live: for(int i=gap;i<arr.lenth;i++){ int j=i; int temp=arr[j] while( j-gap >=0 && arr[j] < arr[j-gap]){ arr[j]=arr[j-gap]; j -=gap; } arr[j]=temp; } } } }
6. Quick sort (important)
: order relation is very important!
From the left, you must start traversing from the right, or you will need one less traversal:
private static void quickSort(int[] arr, int left, int right) { if (left < right) { int partitionIndex = partition(arr, left, right); quickSort(arr, left, partitionIndex - 1); quickSort(arr, partitionIndex + 1, right); } } private static int partition(int[] arr, int left, int right) { int pivot = arr[left]; //After the while loop is terminated, left and right must be equal while (left < right) { while (left < right && arr[right] >= pivot) { --right; } arr[left] = arr[right]; while (left < right && arr[left] <= pivot) { ++left; } arr[right] = arr[left]; } arr[left] = pivot; //right can be changed to left return left; }
Second
ublic static viod sort(int[] arr,int left,int right){ if(left>right){ return; } int baseNumber=arr[left]; int temp=0; //Intermediate variable int l=left,r=right; while(l != r){ while(arr[r]>=baseNumber && l < r){ r--; } while(arr[l]<=baseNumber && l < r){ l++; } if(l < r){ temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; } //Quasi cardinality homing arr[left] = arr[l]; arr[l] = baseNumber; sort(arr,left,l-1); sort(arr,l+1,right); } }
public static void quickSort(int[] arr){ int len = arr.length; if(len ==null ||len == 1 ||len == 0){ return; } sort(arr,0,len-1);}public static void sort(int[] arr,int left, int right){ int base= arr[left]; int l=left,r=right; int temp=0; if(left > right){ return; } while(l != r ){ while(l<r && arr[r] >= base){ r--; } while(l<r && arr[l] <=base){ l++; } if(l<r){ temp=arr[l]; arr[l]=arr[r]; arr[r] =temp; } arr[left]=arr[l]; arr[l]=base; sort(arr,left,l-1); sort(arr,l+1,right); }}
7. Cardinality sorting
Thought: divide and rule
Split into the smallest and fastest, and merge after sorting
public static mergeSort(int[] arr,int left,int right,int[] temp){ int mid=(leth+right)/2; int[] temp=new int[arr.length]; mergeSort(arr,left,mid,temp); mergeSort(arr,mid+1,right,temp); sort(arr,)}
Three major searches
1. Linear search
Idea: just traverse the search directly:
public static int search(int[] arr, int findValue){ for(int i=0;i<arr.length-1;i++){ if(arr[i] == finfValue){ return i; } return -1; }}
2. Binary search:
Idea: find in the middle: pay attention to a number problem
//Note: the premise of binary search is that the array is ordered. public class BinarySearch{ public static void main(String[] args) { int arr[] = { 1, 8, 10, 89, 1000, 1000, 1000, 1234 }; List<Integer> resIndexList = binarySearch(arr, 0, arr.length - 1, 1000); System.out.println("resIndexList=" + resIndexList); } // Complete an after class thinking question: /* * After class thinking questions: {1,8, 10, 89, 1000, 10001234} when there are multiple identical values in an ordered array, how to find all the values, such as here * one thousand * * Train of thought analysis 1. When finding the mid index value, don't return immediately. 2. Scan to the left of the mid index value and add the subscripts of all elements meeting 1000 to the set ArrayList * 3. Scan to the right of mid index value and add the subscripts of all elements satisfying 1000 to the set ArrayList 4. Return ArrayList */ public static List<Integer> binarySearch(int[] arr, int left, int right, int findVal) { // When left > right, it means that the whole array is recursive, but it is not found if (left > right) { return new ArrayList<Integer>(); } int mid = (left + right) / 2; int midVal = arr[mid]; If (findval > midval) {/ / recursive right return binarySearch(arr, mid + 1, right, findVal); } Else if (findval < midval) {/ / left recursion return binarySearch(arr, left, mid - 1, findVal); } else { // Train of thought analysis // 1. When the mid index value is found, do not return it immediately // 2. Scan to the left of the mid index value and add the subscripts of all elements meeting 1000 to the set ArrayList // 3. Scan to the right of mid index value and add the subscripts of all elements satisfying 1000 to the set ArrayList // 4. Return ArrayList to List<Integer> resIndexlist = new ArrayList<Integer>(); // Scan to the left of the mid index value and add the subscripts of all elements satisfying 1000 to the set ArrayList int temp = mid - 1; while (true) { If (temp < 0 | arr [temp]! = findval) {/ / exit break; } // Otherwise, put temp into reindexlist resIndexlist.add(temp); Temp - = 1; / / move temp left } resIndexlist.add(mid); // // Scan to the right of the mid index value and add the subscripts of all elements satisfying 1000 to the set ArrayList temp = mid + 1; while (true) { If (temp > arr.length - 1 | arr [temp]! = findval) {/ / exit break; } // Otherwise, put temp into reindexlist resIndexlist.add(temp); Temp + = 1; / / move temp right } return resIndexlist; } }}
3. Interpolation search
Idea: the formula of mid is changed to find:
int mid = low + (high - low) * (key - arr[low]) / (arr[high] - arr[low]) ;
public static int insertFind(int[] arr,int left,int right,int findValue){ int mid=left+(right-left)*(findValue-arr[left])/(arr[right]-arr[left]); int midVal = arr[mid]; if (findVal > midVal) { // Description should recurse to the right return insertValueSearch(arr, mid + 1, right, findVal); } Else if (findval < midval) {/ / Description: left recursive search return insertValueSearch(arr, left, mid - 1, findVal); } else { return mid; }}
166. Fraction to decimal force deduction (LeetCode) (LeetCode CN. Com)
: direct thinking operation is enough
class Solution { public String fractionToDecimal(int numerator, int denominator) { long a =numerator,b=denominator; if(a%b == 0) return String.valueOf(a/b); StringBuilder sb=new StringBuilder(); // If the judgment symbol is small, it is a positive and a pair of division- if(a*b<0) sb.append('-'); //Turn to positive a=Math.abs(a); b=Math.abs(b); //Splice before decimal point sb.append(String.valueOf(a/b)+'.'); // remainder a%=b; Map<Long,Integer> map=new HashMap<>(); while(a !=0){ // The map stores the key cycle value value: length map.put(a,sb.length()); a*=10; sb.append(a/b); a%=b; if(map.containsKey(a)){ int u =map.get(a); //Regular expression return String.format("%s(%s)",sb.substring(0,u),sb.substring(u)); } } return sb.toString(); } }
628. Maximum product of three numbers - LeetCode (LeetCode CN. Com)
: direct Arrays.sort()~~~
: or linear scan
class Solution { public int maximumProduct(int[] nums) { int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE; // The largest, second and third largest int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE; for(int x:nums){ if(x<min1){ min2=min1; min1=x; }else if(x<min2){ min2=x; } if(x>max1){ max3=max2; max2=max1; max1=x; }else if (x>max2){ max3=max2; max2=x; }else if(x>max3){ max3=x; } } return Math.max((max1*max2*max3),(min1*min2*max1)); } }
88. Merge two ordered arrays - LeetCode (LeetCode CN. Com)
: direct splicing
: or auxiliary array + double pointer
class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m,j = 0; while(i < m + n){ nums1[i++] = nums2[j++]; } Arrays.sort(nums1); }}