Skip to content

Commit ac6ad84

Browse files
committed
Merge remote-tracking branch 'upstream/master'
Merge remote-tracking branch 'upstream/master'
2 parents bc3b4e3 + 91826a7 commit ac6ad84

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2195
-6
lines changed

c-cpp/11_sorts/.gitkeep

Whitespace-only changes.

c-cpp/11_sorts/sorts.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
struct array {
6+
int size;
7+
int used;
8+
int *arr;
9+
};
10+
11+
void dump(struct array *array)
12+
{
13+
int idx;
14+
15+
for (idx = 0; idx < array->used; idx++)
16+
printf("[%02d]: %08d\n", idx, array->arr[idx]);
17+
}
18+
19+
void alloc(struct array *array)
20+
{
21+
array->arr = (int *)malloc(array->size * sizeof(int));
22+
}
23+
24+
void bubble_sort(struct array *array)
25+
{
26+
int i, j;
27+
28+
if (array->used <= 1)
29+
return;
30+
31+
for (i = 0; i < array->used; i++) {
32+
bool has_swap = false;
33+
for (j = 0; j < array->used - i - 1; j++) {
34+
if (array->arr[j] > array->arr[j+1]) {
35+
int tmp;
36+
tmp = array->arr[j];
37+
array->arr[j] = array->arr[j+1];
38+
array->arr[j+1] = tmp;
39+
has_swap = true;
40+
}
41+
42+
}
43+
if (!has_swap)
44+
break;
45+
}
46+
}
47+
48+
void bubble_sort_test()
49+
{
50+
int idx;
51+
struct array ten_int = {10, 0, NULL};
52+
53+
alloc(&ten_int);
54+
for (idx = 0; idx < 10; idx++)
55+
ten_int.arr[idx] = 30 - idx;
56+
ten_int.used = 10;
57+
dump(&ten_int);
58+
bubble_sort(&ten_int);
59+
dump(&ten_int);
60+
}
61+
62+
void insertion_sort(struct array *array)
63+
{
64+
int i, j;
65+
66+
if (array->used <= 1)
67+
return;
68+
69+
for (i = 1; i < array->used; i++) {
70+
int val = array->arr[i];
71+
72+
for (j = i - 1; j >= 0; j--) {
73+
if (val < array->arr[j])
74+
array->arr[j+1] = array->arr[j];
75+
else
76+
break;
77+
}
78+
array->arr[j+1] = val;
79+
}
80+
}
81+
82+
void insertion_sort_test()
83+
{
84+
int idx;
85+
struct array ten_int = {10, 0, NULL};
86+
87+
alloc(&ten_int);
88+
for (idx = 0; idx < 10; idx++)
89+
ten_int.arr[idx] = 30 - idx;
90+
ten_int.used = 10;
91+
dump(&ten_int);
92+
insertion_sort(&ten_int);
93+
dump(&ten_int);
94+
}
95+
96+
void selection_sort(struct array *array)
97+
{
98+
int i, j;
99+
100+
if (array->used <= 1)
101+
return;
102+
103+
for (i = 0; i < array->used - 1; i++) {
104+
int tmp, idx = i;
105+
106+
for (j = i + 1; j < array->used; j++)
107+
if (array->arr[j] < array->arr[idx])
108+
idx = j;
109+
110+
if (idx == i)
111+
continue;
112+
113+
tmp = array->arr[i];
114+
array->arr[i] = array->arr[idx];
115+
array->arr[idx] = tmp;
116+
}
117+
}
118+
119+
void selection_sort_test()
120+
{
121+
int idx;
122+
struct array ten_int = {10, 0, NULL};
123+
124+
alloc(&ten_int);
125+
for (idx = 0; idx < 10; idx++)
126+
ten_int.arr[idx] = 30 - idx;
127+
ten_int.used = 10;
128+
dump(&ten_int);
129+
selection_sort(&ten_int);
130+
dump(&ten_int);
131+
}
132+
133+
int main()
134+
{
135+
//bubble_sort_test();
136+
//selection_sort_test();
137+
insertion_sort_test();
138+
return 0;
139+
}

c-cpp/11_sorts/sorts.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// C program for implementation of selection sort
2+
#include <stdio.h>
3+
4+
void swap(int *xp, int *yp)
5+
{
6+
int temp = *xp;
7+
*xp = *yp;
8+
*yp = temp;
9+
}
10+
11+
void selectionSort(int arr[], int n)
12+
{
13+
int i, j, min_idx;
14+
15+
// One by one move boundary of unsorted subarray
16+
for (i = 0; i < n-1; i++)
17+
{
18+
// Find the minimum element in unsorted array
19+
min_idx = i;
20+
for (j = i+1; j < n; j++)
21+
if (arr[j] < arr[min_idx])
22+
min_idx = j;
23+
24+
// Swap the found minimum element with the first element
25+
swap(&arr[min_idx], &arr[i]);
26+
}
27+
}
28+
29+
/* Function to print an array */
30+
void printArray(int arr[], int size)
31+
{
32+
int i;
33+
for (i=0; i < size; i++)
34+
printf("%d ", arr[i]);
35+
printf("\n");
36+
}
37+
38+
// Driver program to test above functions
39+
int main()
40+
{
41+
int arr[] = {64, 25, 12, 22, 11};
42+
int n = sizeof(arr)/sizeof(arr[0]);
43+
selectionSort(arr, n);
44+
printf("Sorted array: \n");
45+
printArray(arr, n);
46+
return 0;
47+
}

c-cpp/11_sorts/sorts.hpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Created by Liam Huang (Liam0205) on 2018/10/16.
3+
*/
4+
5+
#ifndef SORTS_SORTS_HPP_
6+
#define SORTS_SORTS_HPP_
7+
8+
#include <iterator>
9+
#include <functional>
10+
11+
template <typename BidirIt,
12+
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>>
13+
void bubble_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) {
14+
if (std::distance(first, last) <= 1) { return; }
15+
bool flag = true;
16+
for (auto it = first; flag and it != last; ++it) {
17+
flag = false;
18+
for (auto itt = first; itt != last - std::distance(first, it) - 1; ++itt) {
19+
if (comp(*(itt + 1), *itt)) {
20+
std::swap(*itt, *(itt + 1));
21+
flag = true;
22+
}
23+
}
24+
}
25+
}
26+
27+
template <typename BidirIt,
28+
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>>
29+
void insertion_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) {
30+
if (std::distance(first, last) <= 1) { return; }
31+
for (auto it = first + 1; it != last; ++it) {
32+
const auto target = *it;
33+
auto itt = it;
34+
for (; std::distance(first, itt) > 0 and comp(target, *(itt - 1)); --itt) {
35+
*itt = *(itt - 1);
36+
}
37+
*itt = target;
38+
}
39+
}
40+
41+
template <typename BidirIt,
42+
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>>
43+
void selection_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) {
44+
if (std::distance(first, last) <= 1) { return; }
45+
for (auto it = first; it != last - 1; ++it) {
46+
auto tag = it;
47+
for (auto itt = it + 1; itt != last; ++itt) {
48+
if (comp(*itt, *tag)) {
49+
tag = itt;
50+
}
51+
}
52+
if (tag != it) {
53+
std::swap(*it, *tag);
54+
}
55+
}
56+
}
57+
58+
template <typename FrwdIt,
59+
typename BinaryPred = std::less<typename std::iterator_traits<FrwdIt>::value_type>>
60+
void bubble_down_sort(FrwdIt first, FrwdIt last, BinaryPred comp = BinaryPred()) {
61+
if (std::distance(first, last) <= 1) { return; }
62+
for (auto it = first; it != last; ++it) {
63+
for (auto itt = it + 1; itt != last; ++itt) {
64+
if (comp(*itt, *it)) {
65+
std::swap(*it, *itt);
66+
}
67+
}
68+
}
69+
}
70+
71+
template <typename BidirIt,
72+
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>>
73+
void shell_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) {
74+
const size_t len = std::distance(first, last);
75+
if (len <= 1) { return; }
76+
for (size_t step = len / 2; step >= 1; step /= 2) {
77+
for (auto it = first + step; it != last; ++it) {
78+
auto target = *it;
79+
auto itt = it - step;
80+
for (; std::distance(first, itt) >= 0 and comp(target, *itt); itt -= step) {
81+
*(itt + step) = *itt;
82+
}
83+
*(itt + step) = target;
84+
}
85+
}
86+
}
87+
88+
#endif // SORTS_SORTS_HPP_
89+

c-cpp/11_sorts/sorts_test.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
#include "sorts.hpp"
5+
6+
int main() {
7+
const std::vector<int> test_data{1, 2, 3, 0};
8+
9+
std::vector<int> a(test_data.begin(), test_data.end());
10+
bubble_sort(a.begin(), a.end());
11+
for (auto i : a) {
12+
std::cout << i << ' ';
13+
}
14+
std::cout << '\n';
15+
16+
std::vector<int> b(test_data.begin(), test_data.end());
17+
insertion_sort(b.begin(), b.end());
18+
for (auto i : b) {
19+
std::cout << i << ' ';
20+
}
21+
std::cout << '\n';
22+
23+
std::vector<int> c(test_data.begin(), test_data.end());
24+
selection_sort(c.begin(), c.end());
25+
for (auto i : c) {
26+
std::cout << i << ' ';
27+
}
28+
std::cout << '\n';
29+
30+
std::vector<int> d(test_data.begin(), test_data.end());
31+
bubble_down_sort(d.begin(), d.end());
32+
for (auto i : d) {
33+
std::cout << i << ' ';
34+
}
35+
std::cout << '\n';
36+
37+
std::vector<int> e(test_data.begin(), test_data.end());
38+
shell_sort(e.begin(), e.end());
39+
for (auto i : e) {
40+
std::cout << i << ' ';
41+
}
42+
std::cout << '\n';
43+
44+
return 0;
45+
}

c-cpp/12_sorts/.gitkeep

Whitespace-only changes.

c-cpp/12_sorts/merge_sort.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Created by Liam Huang (Liam0205) on 2018/10/17.
3+
*/
4+
5+
#ifndef SORTS_MERGE_SORT_HPP_
6+
#define SORTS_MERGE_SORT_HPP_
7+
8+
#include <functional>
9+
#include <algorithm>
10+
#include <iterator>
11+
#include <vector>
12+
13+
namespace detail {
14+
template <typename InputIt1, typename InputIt2, typename OutputIt,
15+
typename BinaryPred = std::less<typename std::iterator_traits<InputIt1>::value_type>>
16+
OutputIt merge(InputIt1 first1, InputIt1 last1,
17+
InputIt2 first2, InputIt2 last2,
18+
OutputIt d_first,
19+
BinaryPred comp = BinaryPred()) {
20+
for (; first1 != last1; ++d_first) {
21+
if (first2 == last2) {
22+
return std::copy(first1, last1, d_first);
23+
}
24+
if (comp(*first2, *first1)) {
25+
*d_first = *first2;
26+
++first2;
27+
} else {
28+
*d_first = *first1;
29+
++first1;
30+
}
31+
}
32+
return std::copy(first2, last2, d_first);
33+
}
34+
} // namespace detail
35+
36+
template <typename FrwdIt,
37+
typename BinaryPred = std::less<typename std::iterator_traits<FrwdIt>::value_type>>
38+
void merge_sort(FrwdIt first, FrwdIt last, BinaryPred comp = BinaryPred()) {
39+
const auto len = std::distance(first, last);
40+
if (len <= 1) { return; }
41+
auto cut = first + len / 2;
42+
merge_sort(first, cut, comp);
43+
merge_sort(cut, last, comp);
44+
std::vector<typename std::iterator_traits<FrwdIt>::value_type> tmp;
45+
tmp.reserve(len);
46+
detail::merge(first, cut, cut, last, std::back_inserter(tmp), comp);
47+
std::copy(tmp.begin(), tmp.end(), first);
48+
}
49+
50+
template <typename BidirIt,
51+
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>>
52+
void inplace_merge_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) {
53+
const auto len = std::distance(first, last);
54+
if (len <= 1) { return; }
55+
auto cut = first + len / 2;
56+
merge_sort(first, cut, comp);
57+
merge_sort(cut, last, comp);
58+
std::inplace_merge(first, cut, last, comp);
59+
}
60+
61+
#endif // SORTS_MERGE_SORT_HPP_
62+

0 commit comments

Comments
 (0)