An ACM If the course, taking advantage of the ACM off-season, began to learn the introduction of the algorithm, after a year of ACM learning, gradually discovered, learn something, in-depth is the king, before learning an algorithm, always read the beginning to do the problem, until later found a lot of topics, will algorithm, but do not know is using this algorithm, This is the result of the algorithm understanding is not in place, starting from today, set goals:
1. In-depth system learning algorithms,
2. Learn to write pseudo-code on paper, each chapter of the code to write on the paper first,
3. Each section of the learning notes and algorithms are written on the blog.
On the blog to record their own study notes, easy to review later. Welcome to the great gods passing the wrong place.
Now, first write the first algorithm on the book, insert sort.
Algorithm idea: For an array a[n], the
Insertion_sort#include <iostream>using namespace Std;void insertion_sort (int* A, int n) {for (int j = 1; J < ; N J + +) { int key = A[j]; int i = j-1; Insert A[j] into the sorted sequence a[0..j-1]. while (i >= 0 && a[i] > key) { a[i+1] = a[i]; i--; } A[I+1] = key; }} void print (int* A, int n) {for (int i = 0; i < n; i++) { cout << a[i] << ""; } cout << Endl;} int main () { int a[10] = {43,2,53,1,8,29,52,4,8,10}; cout << "before sorted:"; Print (A, ten); Insertion_sort (A, ten); cout << "after sorted:"; Print (A, ten); return 0;}
Introduction to Algorithms Learning Notes-(1)