How to Become a Thought Leader in Your NicheLeslie Samuel
Are bloggers thought leaders? Here are some tips on how you can become one. Provide great value, put awesome content out there on a regular basis, and help others.
The document discusses and compares linear and binary search algorithms. Linear search sequentially checks each element of an unsorted array to find a target value, while binary search works on a sorted array by repeatedly calculating the midpoint and comparing the target to the value there to narrow the search range. It provides steps for performing a binary search, including sorting the array, calculating the midpoint, and updating the search range based on whether the target is less than, greater than, or equal to the midpoint value.
The linear search algorithm involves checking all elements of an array or data structure sequentially until the target element is found. In the worst case, all elements must be checked, resulting in O(n) time complexity where n is the number of elements. However, if the target is the first element, it requires only constant O(1) time. The algorithm is simple to implement but does not scale well to large data sets as the search time grows linearly with the number of elements.
This document discusses linear search. It contains the names of group members working on the topic. It then defines linear search as checking all elements of an array or structure sequentially until the desired result is found. It provides an example of searching for a phone number in a telephone directory. The document includes pseudocode and C++ code implementations of linear search. It discusses using linear search on a linked list. It covers the advantages of linear search running in constant time if the item is first in the list, and the disadvantages of search time increasing linearly with the number of elements.
The document discusses several sorting algorithms including selection sort, insertion sort, bubble sort, merge sort, and quick sort. It provides details on how each algorithm works including pseudocode implementations and analyses of their time complexities. Selection sort, insertion sort and bubble sort have a worst-case time complexity of O(n^2) while merge sort divides the list into halves and merges in O(n log n) time, making it more efficient for large lists.
How to Become a Thought Leader in Your NicheLeslie Samuel
Are bloggers thought leaders? Here are some tips on how you can become one. Provide great value, put awesome content out there on a regular basis, and help others.
The document discusses and compares linear and binary search algorithms. Linear search sequentially checks each element of an unsorted array to find a target value, while binary search works on a sorted array by repeatedly calculating the midpoint and comparing the target to the value there to narrow the search range. It provides steps for performing a binary search, including sorting the array, calculating the midpoint, and updating the search range based on whether the target is less than, greater than, or equal to the midpoint value.
The linear search algorithm involves checking all elements of an array or data structure sequentially until the target element is found. In the worst case, all elements must be checked, resulting in O(n) time complexity where n is the number of elements. However, if the target is the first element, it requires only constant O(1) time. The algorithm is simple to implement but does not scale well to large data sets as the search time grows linearly with the number of elements.
This document discusses linear search. It contains the names of group members working on the topic. It then defines linear search as checking all elements of an array or structure sequentially until the desired result is found. It provides an example of searching for a phone number in a telephone directory. The document includes pseudocode and C++ code implementations of linear search. It discusses using linear search on a linked list. It covers the advantages of linear search running in constant time if the item is first in the list, and the disadvantages of search time increasing linearly with the number of elements.
The document discusses several sorting algorithms including selection sort, insertion sort, bubble sort, merge sort, and quick sort. It provides details on how each algorithm works including pseudocode implementations and analyses of their time complexities. Selection sort, insertion sort and bubble sort have a worst-case time complexity of O(n^2) while merge sort divides the list into halves and merges in O(n log n) time, making it more efficient for large lists.
Bài 5: Các thuật toán sắp xếp và tìm kiếm cơ bản - Giáo trình FPTMasterCode.vn
Tìm hiểu các giải thuật sắp xếp cơ bản trên cấu trúc dữ
liệu mảng
Tìm hiểu các giải thuật tìm kiếm cơ bản trên cấu trúc dữ
liệu mảng
Đánh giá và so sánh hiệu quả các giải thuật
Bài 5: Các thuật toán sắp xếp và tìm kiếm cơ bản - Giáo trình FPTMasterCode.vn
Tìm hiểu các giải thuật sắp xếp cơ bản trên cấu trúc dữ
liệu mảng
Tìm hiểu các giải thuật tìm kiếm cơ bản trên cấu trúc dữ
liệu mảng
Đánh giá và so sánh hiệu quả các giải thuật
1. Sắp xếp nổi bọt (Bubble Sort)
Ý tưởng
Đi từ trái sang phải mảng A, nếu gặp hai phần tử kề nhau mà không đúng trật tự (khóa của
phần tử trước lớn hơn khóa của phần tử sau) thì hoán vị hai phần tử này. Sau quá trình này A[n]
có giá trị lớn nhất. Thực hiện quá trình trên cho dãy các phần tử từ A[1] tới A[n-1] thì A[n-1] sẽ
có giá trị lớn nhất trong dãy này...Thực hiện cho dãy cuối cùng gồm A[1] và A[2] sẽ được dãy
sắp xếp.
Thuật toán
Giả sử cần sắp xếp dãy gồm n phần tử a[1..n].
Ta đi từ trái qua phải của dãy, nếu gặp 2 phần tử kề nhau mà ngược thứ tự sắp xếp thì ta đổi
chỗ chúng cho nhau. Kết thúc bước đầu tiên, ta được a[n] là phần tử lớn nhất trong dãy, còn
các phần tử nhỏ hơn sẽ được “nổi” dần lên trên. Lặp lại quá trình trên với các đoạn đầu a[1..i]
với i chạy giảm từ n xuống đến 2, ta sẽ nhận được toàn bộ dãy a[1..n] được sắp xếp.
2. Thủ tục
Procedure BubbleSort (var a: mang; n: Integer);
Var i, j: Integer;
Begin
For i:= n downto 2 do
For j:= 1 to i – 1 do
If a[j] > a[j+1] then
Swap (a[j], a[j+1]);
End;
3. Ví dụ.
Sắp xếp dãy sau:
a1 a2 a3 a4 a5 a6 a7
7 1 9 3 8 5 12 i=7
1 7 3 8 5 9 12 i=6
1 3 7 5 8 9 12 i=5
1 3 5 7 8 9 12 i=4
1 3 5 7 8 9 12 i=3
1 3 5 7 8 9 12 i=2
1 3 5 7 8 9 12
Độ phức tạp
Phép toán tích cực: Phép so sánh.
Ở lần lặp thứ i, phép so sánh được thực hiện (i-1) lần.
Ta có:
Tmax(n)=Ttb(n)=Tmin(n)= i=2
n
(i − 1)=
𝑛−1 𝑛
2
=O(n2)
4. Nhận xét
Trường hợp tại bước thứ i, các phần tử đều nằm đứng thứ tự thì ta có thể dừng
thuật toán lại luôn.
Vậy, ta có thể cải tiến thủ tục trên bằng cách thêm vào một biến kiểm tra kiểu
Boolean, biến này sẽ nhận giá trị True nếu dãy đã sắp đúng thứ tự và nhận giá trị
False trong trường hợp ngược lại.
5. Thủ tục cải tiến sẽ như sau:
Procedure Bubble_Sort(var a: mang; n: Integer);
Var i, j: Integer;
Ktr: Boolean;
Begin
i:=n;
ktr:=False;
While (i>=2) and ( not ktr) do
Begin
Ktr:=true;
For j:=1 to i – 1 do
If a[ j ] > a [ j+1 ] then
Begin
Swap (a [ j ], a [ j+1 ]);
Ktr:= false;
End;
Dec(i);
End;
End;