Skip to content

Commit

Permalink
Merge pull request wangzheng0822#78 from Liam0205/11_sorts
Browse files Browse the repository at this point in the history
[C++][11_sorts] 相关排序算法实现
  • Loading branch information
wangzheng0822 committed Oct 23, 2018
2 parents ddcfd3d + fcec521 commit 42b6fd1
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 9 deletions.
Empty file added c-cpp/11_sorts/.gitkeep
Empty file.
89 changes: 89 additions & 0 deletions c-cpp/11_sorts/sorts.hpp
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_

45 changes: 45 additions & 0 deletions c-cpp/11_sorts/sorts_test.cc
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;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
*
*
* 1)泛型动态数组
*
* Author: shi
* Author: shi
*/

public class GenericArray<T> {
private T[] data;
private int size;

// 根据传入容量,构造Array
// 根据传入容量,构造Array
public GenericArray(int capacity) {
data = (T[]) new Object[capacity];
size = 0;
}

// 无参构造方法,默认数组容量为10
// 无参构造方法,默认数组容量为10
public GenericArray() {
this(10);
}
Expand Down Expand Up @@ -57,7 +57,7 @@ public boolean contains(T e) {
return false;
}

// 获取对应元素的下标, 未找到,返回 -1
// 获取对应元素的下标, 未找到,返回 -1
public int find(T e) {
for ( int i = 0; i < size; i++) {
if (data[i].equals(e)) {
Expand All @@ -66,12 +66,12 @@ public int find(T e) {
}
return -1;
}


// 在 index 位置,插入元素e, 时间复杂度 O(m+n)
public void add(int index, T e) {
checkIndex(index);
// 如果当前元素个数等于数组容量,则将数组扩容为原来的2倍
// 如果当前元素个数等于数组容量,则将数组扩容为原来的2倍
if (size == data.length) {
resize(2 * data.length);
}
Expand All @@ -96,7 +96,7 @@ public void addLast(T e) {
// 删除 index 位置的元素,并返回
public T remove(int index) {
checkIndex(index);

T ret = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
Expand Down Expand Up @@ -158,7 +158,7 @@ private void resize(int capacity) {

private void checkIndex(int index) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed! Require index >=0 and index <= size.");
throw new IllegalArgumentException("Add failed! Require index >=0 and index <= size.");
}
}
}

0 comments on commit 42b6fd1

Please sign in to comment.