The smallest number of K for a problem

Source: Internet
Author: User

The sword refers to a question on offer: enter n integers to find the smallest number of K. For example, input 4, 5, 1, 6, 2, 7, 3, 8 total 8, the smallest 4 numbers are: 1, 2, 3, 4.
Analysis: One can take it to the car array sorting problem, first sort the array, and then take the number of k before arrays. In each sorting algorithm, the fast row is cost-effective relatively high, the time event complexity is O (N*LOGN). Is there any other solution?
Solution Two: quick-line ideas come in handy. In the fast algorithm, we usually find a reference element that divides the array into two sub-arrays for this element. The sub-array to the left of the element is less than the element, and the sub-array to the right of the element. That's right! Just find an element so that the number of sub-arrays on his left is K. The worst time complexity of this solution is also O (N*logn), is not much better than the first method of solving!
Solution Three: How can forget the heap platoon, constructs a K element's small top heap, keeps inserting the element inside. The last element in the heap is the smallest element we ask for. The benefit of this solution is that the elements in the array are not moved. But it opens up extra space.
Java solution based on solution Three: the process of building a K-heap is too complex, and Java provides a lot of collections that we can use to scatter! The following code is a small top heap I built with TreeSet, why use TreeSet? Hey, don't delve into, different classmates can look at the red and black trees. TreeSet default sort is a big to small two fork tree, how to do? Rewrite the comparable interface. 1) Customize a class Myinteger, the int is encapsulated, 2) The custom Myinteger implements comparable interface, the size relationship is reversed, 3) the array is inserted into the TreeSet, and the first k elements can be read;
Import Java.util.comparator;import Java.util.iterator;import Java.util.TreeSet;
public class kminnumber{
Static class Myinteger implements Comparable<myinteger> {
Integer i;
Public
myinteger (int i)
  {
this.i=i;
  }
@Override
public
int CompareTo (Myinteger o)
  {
//TODO auto-generated method stub
if (I>o.geti ())
   {
return 1;
   }
Else if (I==o.geti ())
   {
return 0;
   }
Else
return-1;
  }
@Override
Public
String toString ()
  {
//TODO auto-generated method stub
return i.tostring ();
  }
Public
Integer Geti ()
  {
return i;
  }
 }
Public
static void Main (string[] args) {
int[] a=new int[20];
for
(int i = 0; i < a.length; i++)
  {
a[i]= (int) (Math.random () *100);
System.out.print (a[i]+ "");
  }
 
findkmin (a,5);
 }
Public
static void Findkmin (int[] A, int k) {
treeset<myinteger> treeset=new treeset<> ();
for
(int i = 0; i < a.length; i++)
  {
myinteger integer=new Myinteger (A[i]);
treeset.add (integer);
  }
System.out.println ();
int i=0;
iterator<myinteger> iterator=treeset.iterator ();
While
(I<k&&iterator.hasnext ())
  {
i++;
System.out.print ("" +iterator.next (). toString ());
  }
 }
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The smallest number of K for a problem

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.