Академический Документы
Профессиональный Документы
Культура Документы
Республики Молдова
Технический Университет Молдовs
Отчет
по лабораторной работе Nr.2.
Analiza şi proiectarea algoritmilor
Тема: Методы быстрой сортировки и их сравнительный
анализ
Вариант 17
Кишинев – 2023
1 for j←3 to 11
2 do keyA[3] =55
3 ›добавить A[3] к отсортированной части A[1,2] = (56, 83)
4 i←j-1 = 2
5 while 2 > 0 and A[2] = 83 > key = 55
6 A[3]← A[2]=83
7 i ←i - 1 = 1
5 while 1 > 0 and A[1] = 56 > key = 55
6 A[2]← A[1]=56
7 i ←i - 1 = 0
А = (55, 56, 83,9…...)
8 A[1] ←key = 55
1 for j←4 to 11
2 do keyA[4] =9
3 ›добавить A[4] к отсортированной части A[1,2,3] = (55, 56, 83)
4 i←j-1 = 3
5 while 3 > 0 and A[3] = 83 > key = 9
6 A[4]← A[3]=83
7 i ←i - 1 = 2
5 while 2 > 0 and A[2] = 56 > key = 9
6 A[3]← A[2]=56
7 i ←i - 1 = 1
5 while 1 > 0 and A[1] = 55 > key = 9
6 A[2]← A[1]=55
7 i ←i - 1 = 0
1 for j←5 to 11
2 do keyA[5] =55
3 ›добавить A[5] к отсортированной части A[1,2,3,4] = (9,55, 56, 83)
4 i←j-1 = 4
5 while 4 > 0 and A[4] = 83 > key = 55
6 A[5]← A[4]=83
7 i ←i - 1 = 3
5 while 3 > 0 and A[3] = 56 > key = 55
6 A[4]← A[3]=56
7 i ←i - 1 = 2
5 while 2 > 0 and A[2] = 55 не > key = 55
8 A[3] ←key = 55 А = (9,55,55, 56, 83….)
1 for j←6 to 11
2 do keyA[6] =1
3 ›добавить A[6] к отсортированной части A[1,2,3,4,5] = (9,55,55, 56, 83)
4 i←j-1 = 5
5 while 5 > 0 and A[5] = 83 > key = 1
6 A[6]← A[5]=83
7 i ←i - 1 = 4
5 while 4 > 0 and A[4] = 56 > key = 1
6 A[5]← A[4]=56
7 i ←i - 1 = 3
5 while 3 > 0 and A[3] = 55 > key = 1
6 A[4]← A[3]=55
7 i ←i - 1 = 2
5 while 2 > 0 and A[2] = 55 > key = 1
6 A[3]← A[2]=55
7 i ←i - 1 = 1
5 while 1 > 0 and A[1] = 9 > key = 1
6 A[2]← A[1]=9
7 i ←i - 1 = 0
8 A[1] ←key = 1
1 for j←7 to 11
2 do keyA[7] =10
3 ›добавить A[7] к отсортированной части A[1,2,3,4,5,6] = (1,9,55,55, 56,
83)
4 i←j-1 = 6
5 while 6 > 0 and A[6] = 83 > key = 10
6 A[7]← A[6]=83
7 i ←i - 1 = 5
5 while 5 > 0 and A[5] = 56 > key = 10
6 A[6]← A[5]=56
7 i ←i - 1 = 4
5 while 4 > 0 and A[4] = 55 > key = 10
6 A[5]← A[4]=55
7 i ←i - 1 = 3
5 while 3 > 0 and A[3] = 55 > key = 10
6 A[4]← A[3]=55
7 i ←i - 1 = 2
5 while 2 > 0 and A[2] = 9 не> key = 10
8 A[3] ←key = 10
1 for j←8 to 11
2 do keyA[8] =12
3 ›добавить A[8] к отсортированной части
A[1,2,3,4,5,6,7] = (1,9,10,55,55, 56, 83)
4 i←j-1 = 7
5 while 7 > 0 and A[7] = 83 > key = 12
6 A[8]← A[7]=83
7 i ←i - 1 = 6
5 while 6 > 0 and A[6] = 56 > key = 12
6 A[7]← A[6]=56
7 i ←i - 1 = 5
5 while 5 > 0 and A[5] = 55 > key = 12
6 A[6]← A[5]=55
7 i ←i - 1 = 4
5 while 4 > 0 and A[4] = 55 > key = 12
6 A[5]← A[4]=55
7 i ←i - 1 = 3
5 while 3 > 0 and A[3] = 10 не > key = 12
8 A[4] ←key = 12
1 for j←9 to 11
2 do keyA[9] =90
3 ›добавить A[8] к отсортированной части
A[1,2,3,4,5,6,7,8] = (1,9,10,12,55,55, 56, 83)
4 i←j-1 = 8
5 while 8 > 0 and A[8] = 83 > key = 90
8 A[9] ←key = 90
1 for j←10 to 11
2 do keyA[10] =44
3 ›добавить A[10] к отсортированной части
A[1,2,3,4,5,6,7,8.9] = (1,9,10,12,55,55, 56, 83,90)
4 i←j-1 = 9
5 while 9 > 0 and A[9] = 90 > key = 44
6 A[10]← A[9]=90
7 i ←i - 1 = 8
5 while 8 > 0 and A[8] = 83 > key = 44
6 A[9]← A[8]=83
7 i ←i - 1 = 7
5 while 7 > 0 and A[7] = 56 > key = 44
6 A[8]← A[7]=56
7 i ←i - 1 = 6
5 while 6 > 0 and A[6] = 55 > key = 44
6 A[7]← A[6]=55
7 i ←i - 1 = 5
5 while 5 > 0 and A[5] = 55 > key = 44
6 A[6]← A[5]=55
7 i ←i - 1 = 4
5 while 4 > 0 and A[4] = 12 не > key = 44
8 A[5] ←key = 44
1 for j←11 to 11
2 do keyA[11] =32
3 ›добавить A[11] к отсортированной части
A[1,2,3,4,5,6,7,8,9,10] = (1,9,10,12,44,55,55, 56, 83,90)
4 i←j-1 = 10
5 while 10 > 0 and A[10] = 90 > key = 32
6 A[11]← A[10]=90
7 i ←i - 1 = 9
5 while 9 > 0 and A[9] = 83 > key = 32
6 A[10]← A[9]=83
7 i ←i - 1 = 8
5 while 8 > 0 and A[8] = 56 > key = 32
6 A[9]← A[8]=56
7 i ←i - 1 = 7
5 while 7 > 0 and A[7] = 55 > key = 32
6 A[8]← A[7]=55
7 i ←i - 1 = 6
5 while 6 > 0 and A[6] = 55 > key =32
6 A[7]← A[6]=55
7 i ←i - 1 = 5
5 while 5 > 0 and A[5] = 44 > key = 32
6 A[6]← A[5]=55
7 i ←i - 1 = 4
5 while 4 > 0 and A[4] = 12 не > key = 32
8 A[5] ←key = 32 A= (1,9,10,12,32,44,55,55, 56, 83,90)
Псевдокод MergeSort
1 MergeSort(A, p, r):
2 if p < r:
3 q ← [(p + r) // 2]
4 MergeSort(A, p, q)
5 MergeSort(A, q + 1, r)
6 Merge(A, p, q, r)
Merge(A, p, q, r):
7 n1 ← q - p + 1
8 n2 ← r - q
9 left ← new array of size n1
10 right ← new array of size n2
11 for i ← 0 to n1 - 1:
12 left[i] ← A[p + i]
13 for j ← 0 to n2 - 1:
14 right[ j] ← A[q + j + 1]
15 i ← 0
16 j ← 0
17 k ← p
Трассировка:
Исходный массив: A = [56, 83, 55, 9, 55, 1, 10, 12, 90, 44, 32]
1 MergeSort(A, 0, 10)
2 if(0<10)
3 q = (0 + 10) / 2 = 5.
4 Вызов MergeSort(A, 0, 5)
2 if(0<5)
3 q = [(0 + 5) / 2 ]= 2.
4 Вызов MergeSort(A, 0, 2)
2 if(0<2)
3 q = (0 + 2) / 2 = 1.
4 Вызов MergeSort(A, 0, 1)
2 if(0<1)
3 q = (0 + 1) / 2 = 0.
4 Вызов MergeSort(A, 0, 0)
2 if(0<0) False
5 Вызов MergeSort(A, 1, 1)
2 if(1<1) Здесь p = 1 и r = 1, так что условие if не выполняется.
6 merge(A, 0, 0,1) p=0 q=0 r=1
7 n1 =0-0+1=1
8 n2 =1-0=1
9 left ← new array of size n1
10 right ← new array of size n2
11 for i ← 0 to n1 - 1:
12 left[0] ← A[0]=56
13 for j ← 0 to n2 - 1:
14 right[0] ← A[1]=83
15 i ← 0
16 j ← 0
17 k ← p
18 while i < n1 and j < n2: n1=1 n2=1
19 if left[0]=56 ≤ right[0]=83:
20 A[k] ← left[0] k=0
21 i ← i + 1 i=1
24 k ← k + 1 k=1
25 while i < n1:(False)
29 while j < n2: j=0 n2=1
30 A[1] ← right[0]
31 j ← j + 1
32 k ← k + 1
6 merge(A, 0, 1, 2):
7 n1 =2
8 n2 =1
9 left ← new array of size n1
10 right ← new array of size n2
11 for i ← 0 to n1 - 1:
12 left[0] ← A[0]=56
left[1] ← A[1]=83
13 for j ← 0 to n2 - 1:
14 right[0] ← A[2]=55
15 i ← 0
16 j ← 0
17 k ← p
6 merge(A, 3, 3, 4):
7 n1 =1
n2 =1
9 left ← new array of size n1
10 right ← new array of size n2
11 for i ← 0 to n1 - 1:
12 left[0] ← A[3]=9
13 for j ← 0 to n2 - 1:
14 right[0] ← A[4]=55
15 i ← 0
16 j ← 0
17 k ← p
И т.д
Массив на данный момент :
Финальный результат :
Что совпадает с теоретическими данными.
Shellsort =log n исходя из наблюдений
Исходный код :
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[ j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[ j];
j++;
}
k++;
}
int main() {
vector<int> A = {56, 83, 55, 9, 55, 1, 10, 12, 90, 44, 32};
vector<int> B = A; // Копия для каждого алгоритма
return 0;
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[ j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[ j];
j++;
}
k++;
}
int main() {
vector<int> sizes = {5000, 16000, 246000};
return 0;
}