Notes on common basic algorithms

Source: Internet
Author: User
: This article describes common basic algorithm notes. For more information about PHP tutorials, see. Some common basic algorithms (to be continued)

Quick sorting
Int partition (int left, int right, int arr []) {int I = left; int j = right; int value = arr [left]; while (j> I) {// start from j on the right and find a value smaller than value while (j> I & arr [j]> = value) j --; if (j> I) {arr [I] = arr [j]; I ++ ;} // start from the left I and find a value greater than value while (j> I & arr [I] <= value) I ++; if (j> I) {arr [j] = arr [I]; j -- ;}// I = all values greater than values in the j era are displayed on the right, smaller than the value to the left arr [I] = value; return I ;}
void quickSort(int arr[], int left, int right){    if (left < right)    {        int i = partition(left, right,arr);        quickSort(arr, left, i - 1);        quickSort(arr, i + 1, right);    }}void quickSortTest(){    int arr[10] = { 1, 34, 5, 6, 8, 12, 5, 9, 345, 0 };    int n = 10;    cout << "==================quickSort====================" << endl;    for (int i = 0; i <= n - 1; i++)    {        cout << arr[i] << "\t";    }    cout << endl;    quickSort(arr, 0, 9);    for (int i = 0; i <= n - 1; i++)    {        cout << arr[i] << "\t";    }    cout << endl;}
Heap sorting
Include
  
   
// Assume that the left and right subtree of node I is already the largest heap. add the node // I and re-adjust the heid heapAdjust (int arr [], int I, int size) {int l = 2 * I; int r = l + 1; int max = I; if (I <= (size/2 )) {// if (l <= size & arr [l]> arr [max]) max = l; if (r <= size & arr [r]> arr [max]) max = r; if (max! = I) {arr [max] ^ = arr [I]; arr [I] ^ = arr [max]; arr [max] ^ = arr [I]; // int temp = arr [I]; // arr [I] = arr [max]; // arr [max] = temp; heapAdjust (arr, max, size) ;}}// create the maximum heap void buildHeap (int arr [], int heapsize) {int middle = heapsize/2; for (int I = middle; i> = 1; I --) heapAdjust (arr, I, heapsize);} // heapSort (int arr [], int size) {buildHeap (arr, size); for (int I = size; I> = 2; I --) {// arr [I] ^ = arr [1]; // arr [1] ^ = arr [I]; // arr [I] ^ = arr [1]; arr [I] + = arr [1]; arr [1] = arr [I]-arr [1]; arr [I] = arr [I]-arr [1]; heapAdjust (arr, 1, i-1) ;}} void printArr (int arr [], int n) {int I =-1; while (I ++
   
  
Insert sort
Void insertSort () {int arr [10] = {1, 34, 5, 6, 8, 12, 5, 9,345, 0}; int n = 10; cout <"========================== insertSort ================ ===" <endl; for (int I = 0; I <= n-1; I ++) {cout <arr [I] <"\ t" ;}cout <endl; for (int I = 1; I <= n-1; I ++) {int j; int temp = arr [I]; // the inner loop moves forward the sorted and greater than arr [I] for (j = I-1; j> = 0 & arr [j]> temp; j --) {arr [j + 1] = arr [j];} // Finally, place the initial arr [I] value to the blank output location. arr [j + 1] = temp;} for (int I = 0; I <= n-1; I ++) {cout <arr [I] <"\ t ";}}
Select sort
Void selectSort () {int arr [10] = {1, 34, 5, 6, 8, 12, 5, 9,345, 0}; int n = 10; cout <"========================== selectSort ================== ===" <endl; for (int I = 0; I <= n-1; I ++) {cout <arr [I] <"\ t" ;}cout <endl; for (int I = 0; I <= n-2; I ++) {int key = I; // find the subscript for (int j = I + 1; j <= n-1; j ++) {if (arr [j] <arr [key]) {key = j ;}} int value = arr [I]; arr [I] = arr [key]; arr [key] = value;} for (int I = 0; I <= n-1; I ++) {cout <arr [I] <"\ t ";}}
Print binary trees by layer
#include 
   
    Queue q = new Queue();void printBinaryTree(Node *n){    q.put(n);    Node *next = NULL;    while(NULL!=(next=q.get()))    {        if(next->val!=NULL)        std::cout<
    
     value;        if(next->left!=NULL)            q.put(next->left);        if(next->right!=NULL)            q.put(next->right);    }}
    
   
Post-order traversal of binary trees
void postorder(Node root){    if(root == NULL)        return;    postorder(root->left);    postorder(root->right);    visit(root);}void postOrder(Node root){    Stack stack = new Stack();    Node tmp = root;    while(tmp!=NULL || !stack.empty())    {        if(tmp!=NULL)        {            stack.push(tmp,"left");            tmp = tmp->left;        }        else        {            s = stack.pop();            tmp = s.tmp;            tag = s.tag;            if(tag=="right")            {                visit(tmp);                tmp = NULL;            }            else            {                stack.push(tmp,"right");                tmp = tmp->right;            }        }    }}
Single-chain table related (to be improved)
// Reverse void reverse1 (node ** head) {node * temp; node * op; temp = * head; op = temp-> next; (* head) -> next = NULL; while (op) {// Save the next temp of the original op = op-> next; // open the linked list op-> next = * head; // move head * head = op; // move op = temp;} void reverse2 (node ** head) {// use the stack advanced post-release} // Print the void reversePrint (node * head) {if (head-> next! = NULL) {reversePrint (head-> next); printf ("% d \ n", head-> next-> data );}}
Array deduplication
function cleanArray(arr){    var hash = {};    var len = arr.length;    for (var i = 0; i < len; i++) {        if(undefined == hash[arr[i]])            hash[arr[i]] = arr[i];    };    return hash;}//testcasevar arr = [1,2,3,'1','3',45,123,2,3,45,9,9,"test","fadsa","test"];console.log(cleanArray(arr));//output Object {1: 1, 2: 2, 3: 3, 9: 9, 45: 45, 123: 123, test: "test", fadsa: "fadsa"}
Find all character arrays
function permute(strpre, str){  if(str.length==0){    console.log(strpre);  }else{        var l = str.length;    for(var i=0;i
   
Binary search
int binarySearch(int arr[], int l, int r, int k){    while(l<=r){        int m = l+(r-l)/2;        if(arr[m]==k) return m;        if(arr[m]>k) r = m+1;        else l = m-1;    }    return -1;}int binarySearch1(int arr[], int l, int r, int k){    if(r>=l){        int m = l+(r-l)/2;        if(arr[m]==k) return m;        if(arr[m]>k) binarySearch1(arr, l, m-1, k);        else binarySearch1(arr, m+1, r, k);        }       return -1;}
String inversion
# Include
    
     
# Include
     
      
Void reverse1 (char * s) {char * p1, * p2; char c; p1 = s; p2 = s + strlen (s)-1; while (p1
      
       
1) {char c = * s; * s = * (s + tail-1); * (s + tail-1) = c; reverse4 (s + 1, tail-2 );}}
      
     
    
String replication
void copy(char *s, char *d){    while(*s!='\0'){        *d++ = *s++;    }    *d = '\0';}

The above describes the common basic algorithm notes, including the content, and hope to help those who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.