算法总结

1.链表是否有环

struct Node
{
     int iData;
     Node* pNext;
}

bool IsLoop(Node* pHead)
{
     Node* pSlow = pHead;
     Node* pFast = pHead;
     while((NULL!=pFast)&&(NULL!=pFast->pNext))
     {
         pSlow = pSlow->pNext;
         pFast = pFast->pNext->pNext;
         if(pSlow==pFast)
         return true;
     }
     return false;
}

2.链表逆序

递归:

Node* reverseList(Node* pHead)
{
     if((NULL==pHead)||(NULL==pHead->pNext))
         return pHead;

    Node* pNewHead = reverseList(pHead->pNext);
     pHead->pNext->pNext = pHead;
     pHead->pNext = NULL;
     return pNewHead;
}

3.冒泡排序

void bubble_sort(int data[],size_t size){//冒泡

int i,j;

for(i=0;i<size-1;i++){

int order = 1;//设置是否交换的变量,如果交换=0

for(j=0;j<size-1-i;j++){

if(data[j] > data[j+1]){//如果前面比后面大

data[j] = data[j]^data[j+1];

data[j+1]= data[j]^data[j+1];

data[j] = data[j]^data[j+1];

order = 0; //交换 置0

}

}

if(order) break;

}

}

4.快速排序

int Partition(int a[], int low, int high)

{

int x = a[high];

int i = low - 1;

for (int j = low; j < high; j++) {

if (a[j] < x) {

i++;

a[i] = a[i]^a[j];

a[j] = a[i]^a[j];

a[i] = a[i]^a[j];

}

}

a[high] = a[i + 1];

a[i + 1] = x;

return i + 1;

}

void QuickSort(int a[], int low, int high)

{

if (low < high)

{

int q = Partition(a, low, high);

QuickSort(a, low, q - 1);

QuickSort(a, q + 1, high);

}

}

 

其他:https://www.cnblogs.com/shenyantao/p/10659650.html

posted @ 2019-04-13 16:29  shenyantaoit  阅读(237)  评论(0编辑  收藏  举报