常見基礎演算法筆記

來源:互聯網
上載者:User
一些常見的基礎演算法(未完待續)

快速排序

int partition(int left,int right,int arr[]){    int i = left;    int j = right;    int value = arr[left];    while (j > i)    {        //從右邊j開始找到一個比value小的值        while (j > i && arr[j] >= value)            j--;        if (j > i)        {            arr[i] = arr[j];            i++;        }        //從左邊i開始找到一個比value大的值        while (j > i && arr[i] <= value)            i++;        if (j > i)        {            arr[j] = arr[i];            j--;        }    }    //i=j時代表所有比value大的值都到了右邊,比value小的到了左邊    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;}

堆排序

include //假設第i個節點的左右子樹已經是最大堆,加入第//i個節點後重新調整堆void 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);    }    }}//建立最大堆void buildHeap(int arr[], int heapsize){    int middle = heapsize/2;    for(int i=middle;i>=1;i--)        heapAdjust(arr,i,heapsize);}//堆排序void 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++

插入排序

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];        //內迴圈將已排序的且比arr[i]大的往前挪        for (j = i - 1; j >= 0 && arr[j] > temp; j--)        {            arr[j + 1] = arr[j];        }        //最後將最初的arr[i]值放到空出的位置        arr[j + 1] = temp;    }    for (int i = 0; i <= n - 1; i++)    {        cout << arr[i] << "\t";    }}

選擇排序

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;        //內迴圈找到第i大的值的下標        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";    }}

按層列印二叉樹

#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);    }}

後序遍曆二叉樹

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;            }        }    }}

單鏈表相關(待完善)

//單鏈表反轉void reverse1(node **head){    node *temp;    node *op;    temp = *head;    op = temp->next;    (*head)->next = NULL;    while(op)    {        //儲存原始op的下一個        temp = op->next;        //拆開鏈表        op->next = *head;        //移動head        *head = op;        //移動op        op = temp;    }}void reverse2(node **head){    //使用棧先進後出}//反向列印單鏈表void reversePrint(node *head){    if (head->next != NULL)    {        reversePrint(head->next);        printf("%d\n",head->next->data);    }}

數組去重

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"}

求字元數組全排列

function permute(strpre, str){  if(str.length==0){    console.log(strpre);  }else{        var l = str.length;    for(var i=0;i

二分尋找

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;}

字串反轉

#include #include void reverse1(char *s){    char *p1,*p2;    char c;    p1 = s;    p2 = s+strlen(s)-1;    while(p11){        char c = *s;        *s = *(s+tail-1);        *(s+tail-1) = c;        reverse4(s+1,tail-2);    }}

字串複製

void copy(char *s, char *d){    while(*s!='\0'){        *d++ = *s++;    }    *d = '\0';}

以上就介紹了常見基礎演算法筆記,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.