【数据结构】万字详解7种排序算法(英文版)

Conclusion

Method Name Best Time Complexity Worst Time Complexity Ave Time Complexity Space Complexity Is Stable
Merge Sort O(n*log(n)) O(n*log(n)) O(n*log(n)) O(n) Yes
Bubble Sort O(n) O(n**2) O(n**2) O(1) Yes
Insertion Sort O(n) O(n**2) O(n**2) O(1) Yes
Selection Sort O(n**2) O(n**2) O(n**2) O(1) No
Shell Sort O(n) O(n**1.3) O(n**2) O(1) No
Heap Sort O(n*log(n)) O(n*log(n)) O(n*log(n)) O(1) No
Quick Sort O(n*log(n)) O(n*log(n)) O(n**2) O(log(n))~O(n) No

1 Merge Sort

1.1 Definition

A divide-and-conquer algorithm that recursively divides the array into halves, sorts each half, and then merges the sorted halves.

1.2 Easy understanding

let’s imagine you have a bunch of numbered playing cards, and you want to arrange them in order from the smallest to the biggest. But, you can only compare two cards at a time, and it’s a bit tricky to do it all at once. So, here’s what you do:

1.Divide and Conquer: First, you divide the cards into smaller piles. Then, you do the same thing with each of those smaller piles. Keep doing this until each pile only has one card in it.

2.Sort Each Pile: Now, you start combining the piles back together, but in a sorted way. Imagine you have two sorted piles of cards. To combine them, you look at the top cards of each pile and pick the smaller one. You put that card in a new pile. Then, you look again at the top cards of the two piles and pick the smaller one. You keep doing this until all the cards are in the new pile, and it stays sorted.

3.Combine Everything: You do this combining step for all the pairs of piles, then for the groups of four piles, and so on until you have just one big sorted pile. It’s like putting the cards back together, but now they are all in the right order.

Example: Let’s say you have the cards 5, 3, 7, 2, 8, 4 .Here’s how it might work:
Divide: Split the cards into pairs: (5, 3), (7, 2), (8, 4).
Sort Each Pair: Sort each pair separately: (3, 5), (2, 7), (4, 8).
Combine Pairs: Combine the pairs back together, sorting as you go: (2, 3, 5, 7), (4, 8).
Combine Everything: Now, combine those two sorted piles, making one big sorted pile: (2, 3, 4, 5, 7, 8).

1.3 Implemented Java Code

a simple Java implementation of the Merge Sort algorithm:

public class MergeSort {
   public static void main(String[] args) {
       int[] array = {12, 11, 13, 5, 6, 7};

       System.out.println("Original array:");
       printArray(array);

       mergeSort(array);

       System.out.println("\nSorted array:");
       printArray(array);
   }

   // Merge Sort function
   public static void mergeSort(int[] array) {
       int n = array.length;
       if (n > 1) {
           int mid = n / 2;
           int[] leftArray = new int[mid];
           int[] rightArray = new int[n - mid];

           // Copy data to temporary arrays leftArray[] and rightArray[]
           System.arraycopy(array, 0, leftArray, 0, mid);
           System.arraycopy(array, mid, rightArray, 0, n - mid);

           // Recursively sort the two halves
           mergeSort(leftArray);
           mergeSort(rightArray);

           // Merge the sorted halves
           merge(array, leftArray, rightArray);
       }
   }

   // Merge two subarrays of array[]
   public static void merge(int[] array, int[] leftArray, int[] rightArray) {
       int i = 0, j = 0, k = 0;

       // Merge elements back into the original array in sorted order
       while (i < leftArray.length && j < rightArray.length) {
           if (leftArray[i] <= rightArray[j]) {
               array[k] = leftArray[i];
               i++;
           } else {
               array[k] = rightArray[j];
               j++;
           }
           k++;
       }

       // Copy the remaining elements of leftArray[], if there are any
       while (i < leftArray.length) {
           array[k] = leftArray[i];
           i++;
           k++;
       }

       // Copy the remaining elements of rightArray[], if there are any
       while (j < rightArray.length) {
           array[k] = rightArray[j];
           j++;
           k++;
       }
   }

   // Utility function to print an array
   public static void printArray(int[] array) {
       for (int value : array) {
           System.out.print(value + " ");
       }
       System.out.println();
   }
}

1.4 A gif to help u better understand

runoob's gif

ps : This gif is cited from https://www.runoob.com/w3cnote/merge-sort.html.
If it’s a sort , plz contact me to delete that .

1.5 Length and Weakness

the picture below is a time-datasize pic i draw , the gross number of tests is set as 5 .

在这里插入图片描述

We can roughly say that when the data size is small , the elapsed time of merge sort is linearly increasing . However , when a certain threshold is hopped , the time will reversely decrease . So , for for tremendous dataset , merge sort is a good choice.

Strengths:

  1. Stable and efficient with O(n log n) time complexity.
  2. Well-suited for linked lists.

Weaknesses:

  1. Requires additional space for merging.
  2. More complex compared to Bubble Sort or Selection Sort.

Length of Implementation: The implementation tends to be longer, usually around 50-70 lines, due to the recursive nature of the algorithm.

2 Bubble Sort

2.1 Definition

A simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm gets its name because smaller elements “bubble” to the top of the list.

2.2 Easy understanding

Imagine you have a row of kids arranged by their heights, and you want to line them up from the shortest to the tallest.
You start at one end of the row and compare the height of each kid with the one next to them. If a shorter kid is standing next to a taller one, you swap their positions. You keep doing this until you reach the end of the row.

After the first pass, the tallest kid (the maximum height) is guaranteed to be a

  • 25
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
数据结构和算法是计算机科学中非常重要的两个概念。数据结构是一组织和存储数据的方式,而算法是解决问题的步骤和方法。在计算机科学中,有许多经典的数据结构和算法,被广泛应用于各领域。 以下是十大经典数据结构和算法的简要介绍: 1. 数组(Array):是一线性数据结构,可以存储相同类型的元素。数组的访问速度快,但插入和删除操作较慢。 2. 链表(Linked List):也是一线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。链表适用于频繁的插入和删除操作。 3. 栈(Stack):是一后进先出(LIFO)的数据结构,只能在栈顶进行插入和删除操作。 4. 队列(Queue):是一先进先出(FIFO)的数据结构,只能在队尾插入,在队头删除。 5. 树(Tree):是一非线性数据结构,由节点和边组成。树有许多类型,如二叉树、平衡树、堆等。 6. 图(Graph):也是一非线性数据结构,由节点和边组成。图可以表示各实际问题,如网络、社交关系等。 7. 哈希表(Hash Table):使用哈希函数将数据存储在数组中,可以快速查找、插入和删除数据。 8. 排序算法(Sorting Algorithm):如冒泡排序、插入排序、快速排序等,用于将数据按照某规则进行排序。 9. 查找算法(Search Algorithm):如线性查找、二分查找等,用于在数据集中查找特定元素。 10. 图算法(Graph Algorithm):如最短路径算法(Dijkstra算法)、深度优先搜索算法(DFS)、广度优先搜索算法(BFS)等,用于解决图相关的问题。 以上是十大经典数据结构和算法的简要介绍,每个数据结构和算法都有其特点和适用场景,深入学习它们可以帮助我们更好地理解和解决实际问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值