排序演算法 C++實現

來源:互聯網
上載者:User

冒泡排序:

void BubbleSort(int * arr, int size)
{
    int temp;
    int last = size - 1;
    bool sorted = true;

    do {
        sorted = true;
        for (int i = 0; i < last; i++) {

             
            //    swap elements if the higher index element is
            //    greater than the smaller index element
             

            if (arr[i] > arr[i + 1]) {   
                temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
                sorted = false;
            }

        }

        last--;
    } while (!sorted);
    return;
}

 

==========================================

 

 

插入排序:

void InsertionSort(int * arr, int size)
{
     for (int i=1; i < size; i++)
     {
         int v=arr[i];
         int j=i;
         while (j != 0 && arr[j-1]> v)  
         {
             arr[j] = arr[j-1];                    
             j=j-1;
         }
         arr[j] = v;                             
     }
     return;
}

 

 

============================================

 

merge sort(合并排序)

 

// PART ONE COMBINE TWO SORTED ARRAY

static int tempList [ARRAY_SIZE];

void merge(int a[],int p, int q, int r)
{
 int n1=q-p+1;
 int n2=r-q;
 int i,j,k;
 int *left =  new int[n1+1];//&tempList[p];   //   // (int *) malloc((n1+1)*sizeof(int));  //
 int *right = new int[n2+1];//&tempList[q];   //  //(int *) malloc((n2+1)*sizeof(int));
 for (i=0; i<n1; ++i)
 {
  left[i] = a[p+i];
 }
 for ( j=0; j<n2; ++j )
 {
  right[j] = a[q+1+j];
 }
 left[i]=0x7fffffff;
 right[j]=0x7fffffff;
 i=0;
 j=0;
 for ( k=p; k<=r; k++ )
 {
  if ( left[i] <= right[j] )
  {
   a[k] = left[i];
   i++;
  }
  else
  {
   a[k] = right[j];
   j++;
  }
 }
}

// PART TWO MAKE THE ARRAY TO TWO PARTS
void merge_sort(int a[], int p, int r)
{
     if( p < r )
     {
      int q = (p+r)/2;
      merge_sort(a,p,q);
      merge_sort(a,q+1,r);
      merge(a,p,q,r);
     }
}

void MergeSort(int a[], int r)
{
    merge_sort(a, 0, r);
}

=============================

 

快速排序

 

void quickSort(int a[], int l, int r) 

  
    if (l >= r)  
        return; 
 
    int i = l,       
        j = r + 1;   
    int pivot = a[l]; 
 
 
 
    while (true) 
    { 
        do  
        {  
            i = i + 1; 
        } while (a[i] < pivot); 
     
         
        do  
        {   
            j = j - 1; 
        } while (a[j] > pivot); 
     
        if (i >= j)  
            break;  
         
        Swap(a[i], a[j]); 
    } 
 
   a[l] = a[j]; 
   a[j] = pivot; 
 
   quickSort(a, l, j-1);  
   quickSort(a, j+1, r); 

  
void QuickSort(int *a, int n) 

    quickSort(a, 0, n-1); 

void Swap(int& i, int& j)
{
    int temp = 0;
    temp = i;
    i = j;
    j = temp;
}
 ================================

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.