Python provides a simple example of fast sorting algorithm and deduplication

Source: Internet
Author: User
Quick sorting is a basic sorting algorithm. it is quite concise to use Python code, here we will take a look at a simple example of implementing a fast sorting algorithm and removing duplicates in Python: The sorting efficiency is the same as O (N * logN) it is often used because of its high efficiency.

The basic idea of this method is:

1. first, extract a number from the series as the reference number.

2. in the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left.

3. repeat the second step for the left and right intervals until each interval has only one number.

Now we can use an instance to make it clear.

For example, there is an array:

6 2 4 5 3

Step 1: Select a reference number. Don't be scared by this term. you can think of it as a relatively large number, because sorting means comparing the size,

For example, I select the last number 3 as the reference number and compare the number and 3 of the array in sequence. if the number is smaller than 3, I put it on the left and the value is greater than 3 to the right. the following result is displayed:

2 3 6 4 5

Step 2: determine the number of intervals. after the first step, there is only one number in the left interval and no number is compared with it. Therefore, you do not need to repeat the operation. the right interval also has:

6 4 5

Repeat the first step and select 5 as the reference number to obtain the comparison result:

4 5 6

In this way, there is only one number in the left and right intervals, which indicates that the sorting is complete. Finally, all the intervals are merged to obtain the sorting result:

2 3 4 5 6

def quick_sort(array):  less = []; greater = []  if len(array) <= 1:    return array  pivot = array.pop()  for x in array:    if x <= pivot: less.append(x)    else: greater.append(x)  return quick_sort(less) + [pivot] + quick_sort(greater)list = [2,4,2,6,7,8,1]

print quick_sort(list)

[1, 2, 2, 4, 6, 7, 8]

Is it much simpler than C, C #, and JAVA? ^. ^

TIP: Fast sorting of deduplication
As follows, you only need to change the set to a single-value element. here we use Python3 for demonstration:

# -*- coding: utf-8 -*-   import random  L = [2, 3, 8, 4, 9, 5, 6, 5, 6, 10, 17, 11, 2]  def qsort(L):   if len(L)<2: return L   pivot_element = random.choice(L)   small = [i for i in L if i< pivot_element]   #medium = [i for i in L if i==pivot_element]   large = [i for i in L if i> pivot_element]   return qsort(small) + [pivot_element] + qsort(large)  print(qsort(L)) 

Output:

[2, 3, 4, 5, 6, 8, 9, 10, 11, 17] 

You can also directly use the set to sort and deduplicate data.

Mylist = list (set (L) # set automatic string sorting

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.