排序算法和查找算法

1. 查找：在查找表中查看某个特定的记录是否存在

2. 检索：查找某个特定记录的各种属性

3. 插入：将某个不存在的数据元素插入到查找表中

4. 删除：从查找表中删除某个特定元素

ASL=\sum_{i=0}^nP_iC_i

ASL=\sum_{i=0}^nP_iC_i=\frac{1}{5}\sum_{i=0}^nC_i=3

ASL_{success}=\sum_{i=0}^nP_i*(i+1)=\frac{n+1}{2}

ASL_{failed}=n

ASL_{failed}=\frac{1+2+3+...+n+n}{n+1}=\frac{n}{2}+\frac{n}{n+1}

#include <iostream>
#include <cstring>
using namespace std;
class Vector {
private:
int size, length;
int *data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
expand();
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void expand() {
int * old_data = data;
size = size * 2;
data = new int[size];
for (int i = 0; i < length; ++i) {
data[i] = old_data[i];
}
delete[] old_data;
}
int search(int value) {
for(int i = 0; i < length; i++) {
if(data[i] == value) {
return i;
}
else if(data[i] > value) {
return -1;
}
}
return -1;
}
bool remove(int index) {
if (index < 0 || index >= length) {
return false;
}
for (int i = index + 1; i < length; ++i) {
data[i - 1] = data[i];
}
length = length - 1;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
};
int main() {
Vector a(100);
a.insert(0, 2);
a.insert(1, 4);
a.insert(2, 6);
a.insert(3, 8);
a.insert(4, 10);

cout << a.search(4) << endl;
cout << a.search(5) << endl;
return 0;
}

1. 首先确定待查关键字在有序（这里我们假设是升序，即从小到大）的查找表中的范围。通常用两个下标来表示范围：left = 0, right = length - 1

2. 然后用给定的关键字和查找表的正中间位置（下标为 mid = \frac{left + right}{2}）元素的关键字进行比较，若相等，则查找成功；若待查关键字比正中间位置元素的关键字大，则继续对右子表（left = mid + 1）进行折半查找；否则对左子表（right = mid - 1）进行折半查找。

3. 如此重复进行，直到查找成功或范围缩小为空（left > right）即查找不成功为止。

a = (1, 3, 5, 7, 16, 32)

ASL = 1 * \frac{1}{6} + 2 * \frac{2}{6} + 3 * \frac{3}{6} = \frac{7}{3}

ASL = \frac{n+1}{2} = 3 > \frac{7}{3}

#include <iostream>
#include <cstring>
using namespace std;
class Vector {
private:
int size, length;
int *data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
expand();
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void expand() {
int * old_data = data;
size = size * 2;
data = new int[size];
for (int i = 0; i < length; ++i) {
data[i] = old_data[i];
}
delete[] old_data;
}
int search(int value) {
int left = 0, right = length - 1;
while(left <= right) {
int mid = (left + right) / 2;
if(data[mid] == value) {
return mid;
}
else if(data[mid] < value) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}
bool remove(int index) {
if (index < 0 || index >= length) {
return false;
}
for (int i = index + 1; i < length; ++i) {
data[i - 1] = data[i];
}
length = length - 1;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
};
int main() {
Vector a(100);
a.insert(0, 2);
a.insert(1, 4);
a.insert(2, 6);
a.insert(3, 8);
a.insert(4, 10);

cout << a.search(4) << endl;
cout << a.search(5) << endl;
return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
class Vector {
private:
int size, length;
int * data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
return false;
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
void sort() {
for(int i = 0; i < length; i++) {
for(int j = i - 1; j >= 0; j--) {
if(data[j] > data[j + 1]) {
swap(data[j], data[j + 1]);
}
else {
break;
}
}
}
}
};
int main() {
int n;
cin >> n;
Vector arr(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
arr.insert(i, x);
}
arr.sort();
arr.print();
return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
class Vector {
private:
int size, length;
int * data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
return false;
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
void sort() {
for(int i = 0; i < length - 1; i++) {
bool swapped = false;
for(int j = 0; j < length - i -1; j++) {
if(data[j] > data[j + 1]) {
swap(data[j], data[j + 1]);
swapped = true;
}
}
if(swapped == false) {
break;
}
}
}
};
int main() {
int n;
cin >> n;
Vector arr(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
arr.insert(i, x);
}
arr.sort();
arr.print();
return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
class Vector {
private:
int size, length;
int * data, * temp;
void merge_sort(int l, int r) {
if(l == r) {
return;
}
int mid = (l + r) / 2;
merge_sort(l, mid);
merge_sort(mid + 1, r);
int x = l, y = mid + 1, loc = l;
while(x <= mid || y <= r) {
if(x <= mid && (y > r || data[x] <= data[y])) {
temp[loc] = data[x];
x++;
}
else {
temp[loc] = data[y];
y++;
}
loc++;
}
for(int i = l; i <= r; i++) {
data[i] = temp[i];
}
}

public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
temp = new int[size];
}
~Vector() {
delete[] data;
delete[] temp;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
return false;
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
void sort() {
merge_sort(0, length - 1);
}

};
int main() {
int n;
cin >> n;
Vector arr(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
arr.insert(i, x);
}
arr.sort();
arr.print();
return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
class Vector {
private:
int size, length;
int * data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
return false;
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
void sort() {
for(int i = 0; i < length - 1; i++) {
for(int j = i + 1; j < length; j++) {
if(data[i] > data[j]) {
swap(data[i], data[j]);
}
}
}
}
};
int main() {
int n;
cin >> n;
Vector arr(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
arr.insert(i, x);
}
arr.sort();
arr.print();
return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
class Vector {
private:
int size, length;
int * data;
void quick_sort(int l, int r) {
int pivot = data[l], i = l, j = r;
do{
while(i <= j && data[i] < pivot) {
i++;
}
while(i <= j && data[j] > pivot) {
j--;
}
if(i <= j) {
swap(data[i], data[j]);
i++;
j--;
}
}while(i <= j);

if(l < j) {
quick_sort(l, j);
}
if(i < r) {
quick_sort(i, r);
}
}
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
return false;
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
void sort() {
quick_sort(0, length - 1);
}
};
int main() {
int n;
cin >> n;
Vector arr(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
arr.insert(i, x);
}
arr.sort();
arr.print();
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：排序算法和查找算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)