quoniammm / happy-algorithm Goto Github PK
View Code? Open in Web Editor NEW算法导论笔记以及算法实现
License: MIT License
算法导论笔记以及算法实现
License: MIT License
我们自以为知之甚多的的事情背后,无不潜伏着等量的未知因素 ——村上春树
Array.prototype.binary_search = function(low, high, key) {
var mid = parseInt((high + low) / 2),
low = low,
high = high;
if(low > high)
return -1;
else {
if(key == this[mid])
return mid;
else if(key > this[mid])
return this.binary_search(mid+1, high, key);
else
return this.binary_search(low, mid-1, key);
}
}
/*
* chosenSort.cpp
*
* Created on: 2016年1月8日
* Author: quoniam
*/
#include<iostream>
using namespace std;
int main()
{
int n,a[1000];
cin >> n;
for(int i = 0;i < n;i ++) {
cin >> a[i];
}
for(int i = 0;i < n;i ++) {
for(int j = i+1;j < n;j ++) {
if(a[i] < a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i = 0;i < n;i ++) {
cout << a[i] << " ";
}
}
/*
* bubbleSort.cpp
*
* Created on: 2016年1月6日
* Author: quoniam
*/
#include<iostream>
using namespace std;
int main()
{
int n, a[1000];
cin >> n;
for(int i =0;i < n;i ++) {
cin >> a[i];
}
for(int i = 0;i < n;i ++) {
for(int j = 1;j < n - i;j ++) {
if(a[j-1] > a[j]) {
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
for(int i = 0;i < n;i ++) {
cout << a[i] << endl;
}
return 0;
}
/*
* oddEvenSort.cpp
*
* Created on: 2016年1月7日
* Author: quoniam
*/
#include<iostream>
using namespace std;
int main()
{
int a[10];
int l = 0, r = 9;
for(int i = 0;i < 10;i ++) {
cin >> a[i];
}
while(l <= r) {
bool leftisOdd = (a[l] % 2 == 1);
bool rightisEven = (a[r] % 2 == 0);
if(leftisOdd) {
l++;
} else if(rightisEven) {
r--;
} else if(!leftisOdd && !rightisEven) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
}
int start = 0, end = l;
for(int i = start;i < end -1;i ++) {
for(int j = start + 1;j <start + end - i;j ++) {
if(a[j-1] > a[j]) {
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
start = l;
end = 10;
for(int i = start;i < end-1;i ++) {
for(int j = start + 1;j < start + end - i;j ++) {
if(a[j-1] > a[j]) {
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
for(int i =0;i < 10;i ++) {
cout << a[i] << " ";
}
return 0;
}
/*
* oddevenSort.cpp
*
* Created on: 2016年1月7日
* Author: quoniam
*/
#include<iostream>
using namespace std;
int main() {
int a[10];
for(int i = 0;i < 10;i ++) {
cin >> a[i];
}
for(int i = 0;i < 10; i ++) {
for(int j = 1;j < 10 - i;j ++) {
bool leftisEven = (a[j-1] % 2 == 0);
bool rightisEven = (a[j] % 2 == 0);
if((leftisEven && !rightisEven) || (leftisEven == rightisEven && a[j-1] > a[j])){
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
for(int i = 0;i < 10;i ++) {
cout << a[i] << " ";
}
return 0;
}
刚刚好,看到你幸福的样子,于是幸福着你的幸福。 ——村上春树
//归并排序数组 A[1...n]
Array.prototype.merge_sort = function() {
//归并函数,归并函数的结果使得子数组是从小到大排列的
var merge = function(left, right) {
var final = [];
//每次循环弹出两个数组中较小的那个值(因为数组是从小到大排列的),
//并压入要返回的那个数组中
while (left.length && right.length)
final.push(left[0] <= right[0] ? left.shift() : right.shift());
return final.concat(left.concat(right));
};
var len = this.length;
//如果n=1,排序完成
if (len < 2) return this;
var mid = len / 2;
//递归的合并排列好的子数组
return merge(this.slice(0, parseInt(mid)).merge_sort(), this.slice(parseInt(mid)).merge_sort());
};
时间复杂度:O(nlgn)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.