


Top ten sorting algorithms that programmers must master (Part 1)
Sort AlgorithmIt can be said that every programmer must have it After mastering, it is necessary to understand their principles and implementation.The following is an introduction to the Python implementation of the ten most commonly used sorting algorithms to facilitate your learning. .
01 Bubble sort——exchange sort02 Quick sort—— Exchange sorting
03 Selection sort——Selection sorting04 Heap sort ——Select class sort
05 Insertion sort——Insertion class sort
06 Hill sorting-insertion sorting
07 Merging sort-merging sorting
08 Counting sorting-distribution sorting
09 Radix sorting-distribution sorting
10 Bucket sorting-distribution sorting
-
Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the last element should be the largest number.
-
Repeat the above steps for all elements except the last one.
-
Continue repeating the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.
'''冒泡排序''' def Bubble_Sort(arr): for i in range(1, len(arr)): for j in range(0, len(arr)-i): if arr[j] > arr[j+1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr arr = [29, 63, 41, 5, 62, 66, 57, 34, 94, 22] result = Bubble_Sort(arr) print('result list: ', result) # result list: [5, 22, 29, 34, 41, 57, 62, 63, 66, 94]
- First set a dividing value, and divide the array into left and right parts through this dividing value.
- Collect the data that is greater than or equal to the cut-off value to the right of the array, and the data that is less than the cut-off value are concentrated to the left of the array. At this time, each element in the left part is less than or equal to the dividing value, and each element in the right part is greater than or equal to the dividing value.
- #Then, the data on the left and right can be sorted independently. For the array data on the left, you can take a dividing value and divide this part of the data into left and right parts. Similarly, place the smaller value on the left and the larger value on the right. The array data on the right can also be processed similarly.
- # Repeat the above process until the data in the left and right parts are sorted.
'''快速排序''' def Quick_Sort(arr): # 递归入口及出口 if len(arr) >= 2: # 选取基准值,也可以选取第一个或最后一个元素 mid = arr[len(arr) // 2] # 定义基准值左右两侧的列表 left, right = [], [] # 从原始数组中移除基准值 arr.remove(mid) for num in arr: if num >= mid: right.append(num) else: left.append(num) return Quick_Sort(left) + [mid] + Quick_Sort(right) else: return arr arr = [27, 70, 34, 65, 9, 22, 47, 68, 21, 18] result = Quick_Sort(arr) print('result list: ', result) # result list: [9, 18, 21, 22, 27, 34, 47, 65, 68, 70]
- Find the smallest (large) element in the unsorted sequence and store it at the starting position of the sorted sequence.
'''选择排序''' def Selection_Sort(arr): for i in range(len(arr) - 1): # 记录最小数的索引 minIndex = i for j in range(i + 1, len(arr)): if arr[j] < arr[minIndex]: minIndex = j # i 不是最小数时,将 i 和最小数进行交换 if i != minIndex: arr[i], arr[minIndex] = arr[minIndex], arr[i] return arr arr = [5, 10, 76, 55, 13, 79, 49, 51, 65, 30] result = Quick_Sort(arr) print('result list: ', result) # result list: [5, 10, 13, 30, 49, 51, 55, 65, 76, 79]
将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。
从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。
'''插入排序''' def Insertion_Sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and key < arr[j]: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return arr arr = [31, 80, 42, 47, 35, 26, 10, 5, 51, 53] result = Insertion_Sort(arr) print('result list: ', result) # result list: [5, 10, 26, 31, 35, 42, 47, 51, 53, 80]
创建一个堆 H[0……n-1];
把堆首(最大值)和堆尾互换;
把堆的尺寸缩小 1,并调用 shift_down(0),目的是把新的数组顶端数据调整到相应位置;
重复步骤 2,直到堆的尺寸为 1。
'''堆排序''' def Heapify(arr, n, i): largest = i # 左右节点分块 left = 2 * i + 1 right = 2 * i + 2 if left < n and arr[i] < arr[left]: largest = left if right < n and arr[largest] < arr[right]: largest = right if largest != i: # 大小值交换 arr[i], arr[largest] = arr[largest], arr[i] # 递归 Heapify(arr, n, largest) def Heap_Sort(arr): nlen = len(arr) for i in range(nlen, -1, -1): # 调整节点 Heapify(arr, nlen, i) for i in range(nlen - 1, 0, -1): arr[i], arr[0] = arr[0], arr[i] # 调整节点 Heapify(arr, i, 0) return arr arr = [26, 53, 83, 86, 5, 46, 72, 21, 4, 75] result = Heap_Sort(arr) print('result list: ', result) # result list: [4, 5, 21, 26, 46, 53, 72, 75, 83, 86]
The above is the detailed content of Top ten sorting algorithms that programmers must master (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools