-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.cpp
116 lines (92 loc) · 1.64 KB
/
sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include "sort.h"
void insertSort(int arr[], int arrLen)//²åÈëÅÅÐò
{
int key = 0;
int i = 0, j = 0, k;
for(i = 1; i < arrLen; i++)
{
key = arr[i];
j = i -1;
while(j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j+] = key;
}
}
void bubbleSort(int arr [ ],int len)//ðÅÝÅÅÐò
{
int i, j, tmp;
for(i = 0; i < len; i++)
{
for(j = 0; j < len - i - 1; j++)
{
if(a[j] > a[j+1])
{
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
void selectSort((nt arr[], int len)//Ñ¡ÔñÅÅÐò
{
int i , j , max, idenx;
for(i = 0; i < len ; i++)
{
max = i;
for(j = i+1; j < len; j++)
{
if(arr[max] < a[j])
{
max = j;
}
}
if (i != max)
{
int tmp = a[i];
arr[i] = arr[max];
arr[max] = tmp;
}
}
}
void merge(int arr[], int start , int mid, int end)
{
int leftLen = mid - start +1;
int rightLen = end - mid;
int leftArr[leftLen] , rightArr[rightLen];
int i , j , k;
for(i = 0; i < leftLen; i++)
leftArr[i] = arr[start + i];
for(j = 0; j < rightLen; j++)
rightArr[j] = arr[mid + 1 + j];
i = j = k = 0;
while(i < leftLen && j < rightLen)
{
if(leftArr[i] < rightArr[j])
arr[k++] = leftArr[i++];
else
arr[k++] = rightArr[j++];
}
while(i < leftLen)
arr[k++] = leftArr[i++];
while(j < rightLen)
arr[k++] = rightArr[j++];
return 0;
}
void sort(int arr[], int start, int end)
{
int mid;
if(start < end)
{
mid = (start + end) / 2;
sort(start , mid);
sort(mid + 1, end);
merge(arr, start ,mid ,end);
}
return;
}