1) Problem description

Given an unordered set and a number k, find the smallest number in the unordered set.

2) Basic ideas

The idea is to sort out the unordered sets, and then the k-th one from left to right is the answer.

The problem is how to sort. The best way to sort is to find the median of the unordered set, and then use the median for fast sorting. It's ok if you recurse. How to find the median? Divide all the unordered sets into five groups, find the median of each group, then arrange the median of each group into a new set, and the median of this set is the median of the whole unordered set.

3) Code implementation

```package laoya;
import java.lang.Math;
import java.util.Arrays;
import java.util.Scanner;
public class test {
//------------------------------------------------------------------------
public static void swap(int i,int j,int a[]) {//Exchange of elements in an array
int temp = a[i];
a[i]=a[j];
a[j]=temp;
}
public static void BubbleSort(int a[],int low,int high) {//Bubble sort
for(int i=low;i<high;i++) {
for(int j =low;j<high-i;j++) {
if(a[j]>a[j+1]){
swap(j,j+1,a);
}
}
}
}

public static int Sortmiddate1(int a[],int low,int high) {//Median (base) used to find small array (5 bits)
BubbleSort(a,low,high);//Since there are only five elements in the array, you can sort by any order
int mid = (high-low)/2;
return a[mid];                  //Returns the median value
}
//------------------------------------------------------------------------
public static int Sortmiddate(int g[],int low,int high) {//Used to find the median of the whole large array

int p[]=new int[g.length];                 //This method will not change to the original array
for(int gg=0;gg<p.length;gg++) {
p[gg]=g[gg];
}

//The basic idea is to divide all elements into five groups for bubble sorting
int i = (high-low)/5;
if(i==0) {
for(int d=low;d<high;d++) {
for(int j=low;j<=high-d;j++) {
if(p[j]>p[j+1]) {
swap(j,j+1,p);
}
}
}
int od=(low+high)/2;
return p[od];

}
else{
for(int j=0;j<i;j++) {
int t=Sortmiddate1(p,low+(j*5),low+(j*5)+4);
p[j]=t;                      //Place the median of each small array at the beginning of the array
}
int o = Sortmiddate1(p,0,i-1);            //Median of the entire array
return o;                                    //Returns the median value
}

}

public static int Partition(int c[],int i,int j,int key) {//Used for edge dividing

swap(key,0,c);
key=0;

if(i+1==j) {             //Solve two adjacent problems

if(c[i]>c[j]) {
swap(i,j,c);
return key+1;
}
else {
return key;
}

}
else {
int p =i;
int t =j;
while(true) {
while(c[i]<=c[key] && i<j  ) {
i++;
}
while(c[j]>c[key] && j>p) {
j--;
}
if(i<j) {
swap(i,j,c);
}
else {
break;
}
}
swap(key,j,c);
return j;

}

}
//------------------------------------------------------------------------
public static int Import(int p[],int l,int h,int k) {//Recursive solution (finding median and dividing edge)

if(l==h) {                    //End condition (one remaining)
return p[l];
}
else if(l+1==h){              //End condition (2 remaining)
if(p[l]>p[h]) {           //The order of two numbers
swap(l,h,p);
}
if(k-1==l) {
return p[l];
}
else {
return p[h];
}
}

else {
int key =Sortmiddate(p,l,h);  //The key is the median value

for(int i =l;i<=h;i++) {
if(key==p[i]) {
key = i;        //At this time, the key is the subscript of the median
}
}

key = Partition(p,l,h,key);

if(k-1<key) {                       //Judge that the number to be found is about key
int fuck=Import(p,l,key-1,k);
return fuck;
}
else if(k-1==key) {
return p[key];
}
else {
int fuck=Import(p,key+1,h,k);
return fuck;
}
}

}

public static  void main(String[] args) {

int a[]= {1,5,99,3,7,6,4,8,2,100000};
int fuck = Import(a,0,9,10);
System.out.println("The result is"+fuck);
}
}```

4) Time complexity and space complexity

Time complexity
O(NlogN)

Spatial complexity
O(logN)

How to find out the reference random classical fast row.

Published 11 original articles, won praise 0, and visited 234

Tags: Java

Posted on Sat, 01 Feb 2020 11:08:00 -0500 by dwees