Java How to Program学习笔记_第十九章_查询、排序及算法的时间复杂度(Searching, Sorting and Big O)——章节小结(Summary)

这章复习了查询及排序的一些最基本的算法(简单查询法、选择排序法等),以及不同算法的时间复杂度的比较。

Summary

Section 19.1 Introduction

• Searching involves determining if a search key is in the data and, if so, finding its location.

• Sorting involves arranging data into order.

Section 19.2 Linear Search

• The linear search algorithm searches each element in the array sequentially until it finds the correct element, or until it reaches the end of the array without finding the element.

Section 19.3 Big O Notation

• A major difference among searching algorithms is the amount of effort they require.

• Big O notation describes an algorithm’s efficiency in terms of the work required to solve a problem. For searching and sorting algorithms typically it depends on the number of elements in the data.

• An algorithm that’s O(1) does not necessarily require only one comparison. It just means that the number of comparisons does not grow as the size of the array increases.

• An O(n) algorithm is referred to as having a linear run time.

• Big O highlights dominant factors and ignores terms that become unimportant with high n values.

• Big O notation is concerned with the growth rate of algorithm run times, so constants are ignored.

• The linear search algorithm runs in O(n) time.

• The worst case in linear search is that every element must be checked to determine whether the search key exists, which occurs if the search key is the last array element or is not present.

Section 19.4 Binary Search

• Binary search is more efficient than linear search, but it requires that the array be sorted.

• The first iteration of binary search tests the middle element in the array. If this is the search key, the algorithm returns its location. If the search key is less than the middle element, the search continues with the first half of the array. If the search key is greater than the middle element, the search continues with the second half of the array. Each iteration tests the middle value of the remaining array and, if the element is not found, eliminates half of the remaining elements.

• Binary search is a more efficient searching algorithm than linear search because each comparison eliminates from consideration half of the elements in the array.

• Binary search runs in O(log n) time because each step removes half of the remaining elements; this is also known as logarithmic run time.

• If the array size is doubled, binary search requires only one extra comparison.

Section 19.6 Selection Sort

• Selection sort is a simple, but inefficient, sorting algorithm.

• The sort begins by selecting the smallest item and swaps it with the first element. The second iteration selects the second-smallest item (which is the smallest remaining item) and swaps it with the second element. The sort continues until the last iteration selects the second-largest element and swaps it with the second-to-last element, leaving the largest element in the last index. At the ith iteration of selection sort, the smallest i items of the whole array are sorted into the first i indices.

• The selection sort algorithm runs in O(n2) time.

Section 19.7 Insertion Sort

• The first iteration of insertion sort takes the second element in the array and, if it’s less than the first element, swaps it with the first element. The second iteration looks at the third element and inserts it in the correct position with respect to the first two elements. After the ith iteration of insertion sort, the first i elements in the original array are sorted.

• The insertion sort algorithm runs in O(n2) time.

Section 19.8 Merge Sort

• Merge sort is a sorting algorithm that’s faster, but more complex to implement, than selection sort and insertion sort. The merge sort algorithm sorts an array by splitting it into two equalsized subarrays, sorting each subarray recursively and merging the subarrays into one larger array.

• Merge sort’s base case is an array with one element. A one-element array is already sorted, so merge sort immediately returns when it’s called with a one-element array. The merge part of merge sort takes two sorted arrays and combines them into one larger sorted array.

• Merge sort performs the merge by looking at the first element in each array, which is also the smallest element in the array. Merge sort takes the smallest of these and places it in the first element of the larger array. If there are still elements in the subarray, merge sort looks at the second of these (which is now the smallest element remaining) and compares it to the first element in the other subarray. Merge sort continues this process until the larger array is filled.

• In the worst case, the first call to merge sort has to make O(n) comparisons to fill the n slots in the final array.

• The merging portion of the merge sort algorithm is performed on two subarrays, each of approximately size n/2. Creating each of these subarrays requires n /2 – 1 comparisons for each subarray, or O(n) comparisons total. This pattern continues as each level works on twice as many arrays, but each is half the size of the previous array.

• Similar to binary search, this halving results in log n levels for a total efficiency of O(n log n).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值