Interviewer: You have been working for 3 years, and you don’t even know how to choose a ranking method. How can I choose you?

Click above↑「Love Development」Follow us
At 10 o'clock every night, capture technical thinking and entrepreneurial resource insights


In interviews, algorithms are often encountered, especially some common algorithms.
 
Zhang Gong has graduated for 3 years and has been doing java development in a startup company. Recently, I went to an interview with a well-known Internet company. After doing a written test, the interviewer thought it was pretty good, so he wanted to further investigate Zhang Gong’s coding ability. He hand-written the selection and sorting algorithm, but Zhang Gong couldn't write it out. The interviewer said: You have graduated for 3 years, and you can't even write a selection and sorting method. This will be the case when I take the computer rank exam. Zhang Gong looked helpless, but he really shouldn't. Basic algorithms like selection sorting should be mastered normally.
 
The written test I took before also encountered the situation of handwriting sorting algorithm. About the selection sorting may basically be able to write, but if the program can be optimized, it will be better, I believe it can leave a better for the interviewer impression.
 
Let's first look at what selection ordering method:

Select sort method is an unstable sorting algorithm. Its working principle is to select the smallest (or largest) element from the data elements to be sorted each time, store it at the beginning of the sequence, and then continue to search for the smallest (large) element from the remaining unsorted elements. Then put it at the end of the sorted sequence. And so on, until all the data elements to be sorted are arranged.
  640?wx_fmt=gif
 
stability
The selection sort is to select the smallest element for each position, such as the smallest for the first position, the second smallest for the second element among the remaining elements, and so on, until the n-1th element, the first There is no need to select n elements, because only its largest element is left. Then, in a selection, if an element is smaller than the current element, and the smaller element appears behind an element equal to the current element, the stability after the exchange is destroyed.
 
For example, in the sequence 6 8 6 3 9, we know that the first element 6 will be exchanged with 3 in the first pass, then the relative order of the two 6s in the original sequence is destroyed, so the selection order is unstable Sorting algorithm.
 
About time complexity
The exchange operation of the selection sort is between 0 and (n-1) times. The comparison operation of selection sort is between n (n-1) / 2 times. The assignment operation for selection sort is between 0 and 3 (n-1) times. The number of comparisons O(n^2), the number of comparisons has nothing to do with the initial state of the keyword, the total number of comparisons N=(n-1)+(n-2)+...+1=n*(n-1) /2. The number of exchanges is O(n). In the best case, it has been ordered and exchanged 0 times; in the worst case, it exchanges n-1 times and exchanges in reverse order n/2 times. The number of exchanges is much less than that of bubble sort. Because the CPU time required for exchange is more than the CPU time required for comparison, when the value of n is small, selective sort is faster than bubble sort. The time complexity of direct selection sorting is O(n*n), and the space complexity is O(1). The time complexity of tree selection sorting is O(nlog2n), and the space complexity is O(n). The average time complexity of heap sort is O(nlog2n), which is highly efficient, but the implementation is relatively complicated, and the space cost is O(1).

Let's look at the selection sort algorithm code written in java


 
  
/**
* Selection sort
*
* @param arr
*/

public static void selectionSort ( int [] arr) {
int min;
int length = arr.length;
for ( int i = 0 ; i <length; i++) {
// Initialize the smallest data array subscript in the unsorted sequence
min = i;
for ( int j = i + 1 ; j <length; j++) {
// Continue to find the smallest element among the unsorted elements and save its subscript
if (arr [j] <arr[min]) {
min = j;
}
}
// Put the smallest element in the unsorted column at the end of the sorted column
if (min != i) {
swap(min, i, arr);
}
}
}

/**
* Swap order
*
* @param x
* @param y
* @param arr
*/

private static void swap ( int x, int y, int [] arr) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}

Call instance
 
public static void main(String[] args) {
int arr[] = { 2, 17, 2, 11, 7, 6, 19, 9, 14, 20, 17, 13, 14, 2, 10};
System.out.println(String.format( "original array = %s\n", Arrays.toString(arr)));
selectionSort(arr);
System.out.println(String.format( "sorted array = %s\n", Arrays.toString(arr)));
}
Output result
640?wx_fmt=png

For the selection and sorting algorithm, if it is still not easy to understand, let's take a look at the animation diagram, which will be easier to understand.


640?wx_fmt=gif

I wonder if you fully understand the selection sorting algorithm.

Basically, the selective sorting method is quite good. When the data size is not large, it is still recommended, but compared to other sorting algorithms (such as quick sort), the efficiency is still obviously insufficient.
 
The editor believes that the idea of ​​selective ranking method is still very important and should be paid attention to.

-END-
Selected recommendations in the past










Pay more attention to exciting content, please long press to identify the attention

Guess you like

Origin blog.csdn.net/X8i0Bev/article/details/101016000