バブルソート、選択ソート、挿入ソート、クイックソート、集計ソート、STLアルゴリズムにおけるsortソート(C++/C言語実装)
バブルソート(小から大まで):
ソートの選択(小から大まで):
ソートの挿入(小から大まで):
クイックソート(小から大まで):
集計ソート(小から大まで):
STLのsortソート:
#include
#include
int main() {
int a[10] = {
13, 25, 12, 98, 56, 46, 37, 61, 59, 46};
int i, j, temp;
for (i = 0; i < 10; i++) {
for (j = 0; j < i; j++) {
if (a[j] > a[i])
{
temp = a[j]; a[j] = a[i];a[i] = temp;}// ,
}
}
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("
");
return 0;
}
/*
:
12 13 25 37 46 46 56 59 61 98
*/
ソートの選択(小から大まで):
#include
#include
using namespace std;
int main() {
int a[10] = {
13, 25, 12, 98, 56, 46, 37, 61, 59, 46};
int i, j, temp_p, temp;
for (i = 0; i < 9; i++) {
// , i ,
temp_p = i;
for (j = i + 1; j < 10; j++) {
// i i ,
if (a[j] < a[temp_p]) {
temp_p = j;}
}
temp = a[i];a[i] = a[temp_p];a[temp_p] = temp;// i ,
}
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("
");
return 0;
}
/*
:
12 13 25 37 46 46 56 59 61 98
*/
ソートの挿入(小から大まで):
#include
#include
using namespace std;
int main() {
int a[10] = {
13, 25, 12, 98, 56, 46, 37, 61, 59, 46};
int i, j, k, temp;
for (i = 1; i <= 9; i++) {
//a[0]~a[i-1] ,a[i]~a[9] 。
for (j = 0; j < i; j++)
if (a[j] > a[i]) {
// ,
temp = a[i];//
for (k = i; k > j; k--) {
a[k] = a[k - 1];}//
a[j] = temp;//
break;// ,
}
}
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("
");
return 0;
}
/*
:
12 13 25 37 46 46 56 59 61 98
*/
クイックソート(小から大まで):
#include
#include
using namespace std;
void Swap(int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
void Quicksort(int a[], int s, int e) {
if (s >= e) return;
int i, j, k;
k = a[s];
i = s, j = e;
while (i != j) {
while (i < j && a[j] >= k) j--;// , k
Swap(a[i], a[j]);// k k
while (i < j && a[i] <= k) i++;// , k
Swap(a[i], a[j]);// k k
}
Quicksort(a, s, i - 1);
Quicksort(a, i + 1, e);// a , , ,
}
int main() {
int a[10] = {
13, 25, 12, 98, 56, 46, 37, 61, 59, 46};
int i, s = 0, e;
e = sizeof(a) / sizeof(int) - 1;
Quicksort(a, s, e);
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("
");
system("pause");
return 0;
}
/*
:
12 13 25 37 46 46 56 59 61 98
*/
集計ソート(小から大まで):
#include
#include
using namespace std;
void merge(int a[], int s, int m, int e, int temp[]) {
//
int ps, pm, pt, i;
ps = s, pm = m + 1, pt = 0;//
while (ps <= m && pm <= e) {
if (a[ps] < a[pm])
temp[pt++] = a[ps++];//
else
temp[pt++] = a[pm++];//
}
while (ps <= m)
temp[pt++] = a[ps++];//
while (pm <= e)
temp[pt++] = a[pm++];//
for (i = 0; i < e - s + 1; i++)
a[s + i] = temp[i]; // a
}
void mergesort(int a[], int s, int e, int b[]) {
//
int m;
if (s < e) {
m = s + (e - s) / 2;//
mergesort(a, s, m, b);
mergesort(a, m + 1, e, b);
merge(a, s, m, e, b);
}
}
int main() {
int a[10] = {
13, 25, 12, 98, 56, 46, 37, 61, 59, 46};
int b[10], s = 0, e, i;
e = sizeof(a) / sizeof(int) - 1;
mergesort(a, s, e, b);
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("
");
system("pause");
return 0;
}
/*
:
12 13 25 37 46 46 56 59 61 98
*/
STLのsortソート:
/*
STL:standard template library , #include
*/
using namespace std;
#include
#include
struct sort_self {
// ,int
bool operator()(const int &a1, const int &a2) const {
if (a1 > a2)//
return true;
else
return false;
}
};
int main() {
int a[10] = {
13, 25, 12, 98, 56, 46, 37, 61, 59, 46};
int n, i;
n = sizeof(a) / sizeof(int);
sort(a, a + n);
cout << " " << endl;
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
cout << endl;
sort(a, a + n, greater<int>());
cout << " " << endl;
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
cout << endl;
cout << " " << endl;
sort(a, a + n, sort_self());
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
cout << endl;
return 0;
}
/*
:
12 13 25 37 46 46 56 59 61 98
98 61 59 56 46 46 37 25 13 12
98 61 59 56 46 46 37 25 13 12
*/