forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
50 changed files
with
2,195 additions
and
6 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
struct array { | ||
int size; | ||
int used; | ||
int *arr; | ||
}; | ||
|
||
void dump(struct array *array) | ||
{ | ||
int idx; | ||
|
||
for (idx = 0; idx < array->used; idx++) | ||
printf("[%02d]: %08d\n", idx, array->arr[idx]); | ||
} | ||
|
||
void alloc(struct array *array) | ||
{ | ||
array->arr = (int *)malloc(array->size * sizeof(int)); | ||
} | ||
|
||
void bubble_sort(struct array *array) | ||
{ | ||
int i, j; | ||
|
||
if (array->used <= 1) | ||
return; | ||
|
||
for (i = 0; i < array->used; i++) { | ||
bool has_swap = false; | ||
for (j = 0; j < array->used - i - 1; j++) { | ||
if (array->arr[j] > array->arr[j+1]) { | ||
int tmp; | ||
tmp = array->arr[j]; | ||
array->arr[j] = array->arr[j+1]; | ||
array->arr[j+1] = tmp; | ||
has_swap = true; | ||
} | ||
|
||
} | ||
if (!has_swap) | ||
break; | ||
} | ||
} | ||
|
||
void bubble_sort_test() | ||
{ | ||
int idx; | ||
struct array ten_int = {10, 0, NULL}; | ||
|
||
alloc(&ten_int); | ||
for (idx = 0; idx < 10; idx++) | ||
ten_int.arr[idx] = 30 - idx; | ||
ten_int.used = 10; | ||
dump(&ten_int); | ||
bubble_sort(&ten_int); | ||
dump(&ten_int); | ||
} | ||
|
||
void insertion_sort(struct array *array) | ||
{ | ||
int i, j; | ||
|
||
if (array->used <= 1) | ||
return; | ||
|
||
for (i = 1; i < array->used; i++) { | ||
int val = array->arr[i]; | ||
|
||
for (j = i - 1; j >= 0; j--) { | ||
if (val < array->arr[j]) | ||
array->arr[j+1] = array->arr[j]; | ||
else | ||
break; | ||
} | ||
array->arr[j+1] = val; | ||
} | ||
} | ||
|
||
void insertion_sort_test() | ||
{ | ||
int idx; | ||
struct array ten_int = {10, 0, NULL}; | ||
|
||
alloc(&ten_int); | ||
for (idx = 0; idx < 10; idx++) | ||
ten_int.arr[idx] = 30 - idx; | ||
ten_int.used = 10; | ||
dump(&ten_int); | ||
insertion_sort(&ten_int); | ||
dump(&ten_int); | ||
} | ||
|
||
void selection_sort(struct array *array) | ||
{ | ||
int i, j; | ||
|
||
if (array->used <= 1) | ||
return; | ||
|
||
for (i = 0; i < array->used - 1; i++) { | ||
int tmp, idx = i; | ||
|
||
for (j = i + 1; j < array->used; j++) | ||
if (array->arr[j] < array->arr[idx]) | ||
idx = j; | ||
|
||
if (idx == i) | ||
continue; | ||
|
||
tmp = array->arr[i]; | ||
array->arr[i] = array->arr[idx]; | ||
array->arr[idx] = tmp; | ||
} | ||
} | ||
|
||
void selection_sort_test() | ||
{ | ||
int idx; | ||
struct array ten_int = {10, 0, NULL}; | ||
|
||
alloc(&ten_int); | ||
for (idx = 0; idx < 10; idx++) | ||
ten_int.arr[idx] = 30 - idx; | ||
ten_int.used = 10; | ||
dump(&ten_int); | ||
selection_sort(&ten_int); | ||
dump(&ten_int); | ||
} | ||
|
||
int main() | ||
{ | ||
//bubble_sort_test(); | ||
//selection_sort_test(); | ||
insertion_sort_test(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// C program for implementation of selection sort | ||
#include <stdio.h> | ||
|
||
void swap(int *xp, int *yp) | ||
{ | ||
int temp = *xp; | ||
*xp = *yp; | ||
*yp = temp; | ||
} | ||
|
||
void selectionSort(int arr[], int n) | ||
{ | ||
int i, j, min_idx; | ||
|
||
// One by one move boundary of unsorted subarray | ||
for (i = 0; i < n-1; i++) | ||
{ | ||
// Find the minimum element in unsorted array | ||
min_idx = i; | ||
for (j = i+1; j < n; j++) | ||
if (arr[j] < arr[min_idx]) | ||
min_idx = j; | ||
|
||
// Swap the found minimum element with the first element | ||
swap(&arr[min_idx], &arr[i]); | ||
} | ||
} | ||
|
||
/* Function to print an array */ | ||
void printArray(int arr[], int size) | ||
{ | ||
int i; | ||
for (i=0; i < size; i++) | ||
printf("%d ", arr[i]); | ||
printf("\n"); | ||
} | ||
|
||
// Driver program to test above functions | ||
int main() | ||
{ | ||
int arr[] = {64, 25, 12, 22, 11}; | ||
int n = sizeof(arr)/sizeof(arr[0]); | ||
selectionSort(arr, n); | ||
printf("Sorted array: \n"); | ||
printArray(arr, n); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Created by Liam Huang (Liam0205) on 2018/10/16. | ||
*/ | ||
|
||
#ifndef SORTS_SORTS_HPP_ | ||
#define SORTS_SORTS_HPP_ | ||
|
||
#include <iterator> | ||
#include <functional> | ||
|
||
template <typename BidirIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>> | ||
void bubble_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { | ||
if (std::distance(first, last) <= 1) { return; } | ||
bool flag = true; | ||
for (auto it = first; flag and it != last; ++it) { | ||
flag = false; | ||
for (auto itt = first; itt != last - std::distance(first, it) - 1; ++itt) { | ||
if (comp(*(itt + 1), *itt)) { | ||
std::swap(*itt, *(itt + 1)); | ||
flag = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
template <typename BidirIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>> | ||
void insertion_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { | ||
if (std::distance(first, last) <= 1) { return; } | ||
for (auto it = first + 1; it != last; ++it) { | ||
const auto target = *it; | ||
auto itt = it; | ||
for (; std::distance(first, itt) > 0 and comp(target, *(itt - 1)); --itt) { | ||
*itt = *(itt - 1); | ||
} | ||
*itt = target; | ||
} | ||
} | ||
|
||
template <typename BidirIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>> | ||
void selection_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { | ||
if (std::distance(first, last) <= 1) { return; } | ||
for (auto it = first; it != last - 1; ++it) { | ||
auto tag = it; | ||
for (auto itt = it + 1; itt != last; ++itt) { | ||
if (comp(*itt, *tag)) { | ||
tag = itt; | ||
} | ||
} | ||
if (tag != it) { | ||
std::swap(*it, *tag); | ||
} | ||
} | ||
} | ||
|
||
template <typename FrwdIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<FrwdIt>::value_type>> | ||
void bubble_down_sort(FrwdIt first, FrwdIt last, BinaryPred comp = BinaryPred()) { | ||
if (std::distance(first, last) <= 1) { return; } | ||
for (auto it = first; it != last; ++it) { | ||
for (auto itt = it + 1; itt != last; ++itt) { | ||
if (comp(*itt, *it)) { | ||
std::swap(*it, *itt); | ||
} | ||
} | ||
} | ||
} | ||
|
||
template <typename BidirIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>> | ||
void shell_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { | ||
const size_t len = std::distance(first, last); | ||
if (len <= 1) { return; } | ||
for (size_t step = len / 2; step >= 1; step /= 2) { | ||
for (auto it = first + step; it != last; ++it) { | ||
auto target = *it; | ||
auto itt = it - step; | ||
for (; std::distance(first, itt) >= 0 and comp(target, *itt); itt -= step) { | ||
*(itt + step) = *itt; | ||
} | ||
*(itt + step) = target; | ||
} | ||
} | ||
} | ||
|
||
#endif // SORTS_SORTS_HPP_ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "sorts.hpp" | ||
|
||
int main() { | ||
const std::vector<int> test_data{1, 2, 3, 0}; | ||
|
||
std::vector<int> a(test_data.begin(), test_data.end()); | ||
bubble_sort(a.begin(), a.end()); | ||
for (auto i : a) { | ||
std::cout << i << ' '; | ||
} | ||
std::cout << '\n'; | ||
|
||
std::vector<int> b(test_data.begin(), test_data.end()); | ||
insertion_sort(b.begin(), b.end()); | ||
for (auto i : b) { | ||
std::cout << i << ' '; | ||
} | ||
std::cout << '\n'; | ||
|
||
std::vector<int> c(test_data.begin(), test_data.end()); | ||
selection_sort(c.begin(), c.end()); | ||
for (auto i : c) { | ||
std::cout << i << ' '; | ||
} | ||
std::cout << '\n'; | ||
|
||
std::vector<int> d(test_data.begin(), test_data.end()); | ||
bubble_down_sort(d.begin(), d.end()); | ||
for (auto i : d) { | ||
std::cout << i << ' '; | ||
} | ||
std::cout << '\n'; | ||
|
||
std::vector<int> e(test_data.begin(), test_data.end()); | ||
shell_sort(e.begin(), e.end()); | ||
for (auto i : e) { | ||
std::cout << i << ' '; | ||
} | ||
std::cout << '\n'; | ||
|
||
return 0; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Created by Liam Huang (Liam0205) on 2018/10/17. | ||
*/ | ||
|
||
#ifndef SORTS_MERGE_SORT_HPP_ | ||
#define SORTS_MERGE_SORT_HPP_ | ||
|
||
#include <functional> | ||
#include <algorithm> | ||
#include <iterator> | ||
#include <vector> | ||
|
||
namespace detail { | ||
template <typename InputIt1, typename InputIt2, typename OutputIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<InputIt1>::value_type>> | ||
OutputIt merge(InputIt1 first1, InputIt1 last1, | ||
InputIt2 first2, InputIt2 last2, | ||
OutputIt d_first, | ||
BinaryPred comp = BinaryPred()) { | ||
for (; first1 != last1; ++d_first) { | ||
if (first2 == last2) { | ||
return std::copy(first1, last1, d_first); | ||
} | ||
if (comp(*first2, *first1)) { | ||
*d_first = *first2; | ||
++first2; | ||
} else { | ||
*d_first = *first1; | ||
++first1; | ||
} | ||
} | ||
return std::copy(first2, last2, d_first); | ||
} | ||
} // namespace detail | ||
|
||
template <typename FrwdIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<FrwdIt>::value_type>> | ||
void merge_sort(FrwdIt first, FrwdIt last, BinaryPred comp = BinaryPred()) { | ||
const auto len = std::distance(first, last); | ||
if (len <= 1) { return; } | ||
auto cut = first + len / 2; | ||
merge_sort(first, cut, comp); | ||
merge_sort(cut, last, comp); | ||
std::vector<typename std::iterator_traits<FrwdIt>::value_type> tmp; | ||
tmp.reserve(len); | ||
detail::merge(first, cut, cut, last, std::back_inserter(tmp), comp); | ||
std::copy(tmp.begin(), tmp.end(), first); | ||
} | ||
|
||
template <typename BidirIt, | ||
typename BinaryPred = std::less<typename std::iterator_traits<BidirIt>::value_type>> | ||
void inplace_merge_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { | ||
const auto len = std::distance(first, last); | ||
if (len <= 1) { return; } | ||
auto cut = first + len / 2; | ||
merge_sort(first, cut, comp); | ||
merge_sort(cut, last, comp); | ||
std::inplace_merge(first, cut, last, comp); | ||
} | ||
|
||
#endif // SORTS_MERGE_SORT_HPP_ | ||
|
Oops, something went wrong.