ÓãCÂÛ̳

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á
²é¿´: 2175|»Ø¸´: 4

C++ ¿ìËÙÅÅÐò µÄÎÊÌ⣡£¡£¡£¡ÇóÖúÇóÖú¡£

[¸´ÖÆÁ´½Ó]
·¢±íÓÚ 2020-3-29 00:13:19 | ÏÔʾȫ²¿Â¥²ã |ÔĶÁģʽ

ÂíÉÏ×¢²á£¬½á½»¸ü¶àºÃÓÑ£¬ÏíÓøü¶à¹¦ÄÜ^_^

ÄúÐèÒª µÇ¼ ²Å¿ÉÒÔÏÂÔØ»ò²é¿´£¬Ã»ÓÐÕ˺ţ¿Á¢¼´×¢²á

x
±¾Ìû×îºóÓÉ Ê®¶þÔµÄÏô°î ÓÚ 2020-3-30 13:13 ±à¼­

ºÍ¼×Óã¸çÄǸö¿ìËÙÅÅÐòºÃÏñÓеãÇø±ð£¿£¿£¿

ÒªÇóÓõ½Èý¸öº¯Êý£º

void quicksort( int * const, int, int );
int partition( int * const, int, int );
void swap( int * const, int * const );

quicksortÊǵݹ麯Êý£¬µ÷ÓÃpartitionº¯Êý½øÐзÖÇø¡£


û̫Àí½âÓÃÈý¸öº¯ÊýÕâÑùÔõô×ö£¬Ð´ÁËÒ»°ë²»»áÁË£¬²»ÖªµÀ˼·¶Ô²»¶Ô¡£
Çó½â´ð¡£

ÎÒдµÄÈçÏ£º





#include<iostream>
using namespace std;

void quickSort(int* const, int, int);
int partition(int* const, int, int);
void swap(int&,int&);



void quickSort(int* const a, int left, int right)
{
        if (left >= right)return;
        int pivot = partition(a, left, right);

        quickSort(a, left, pivot - 1);
        quickSort(a, pivot + 1, right);

}



int partition(int* const a, int l, int r)//¶Ô´Óa[l]µ½a[r]µÄÊý×飬ÕÒµ½a[l]µÄλÖ㬲¢·µ»ØlµÄÖµ
{

        while (l < r)
        {


               
        }

        return l;
}



void swap(int &x,int &y)
{
        int temp = x;
        x = y;
        y = temp;

}
       


int main()
{
        int a[10] = { 6,5,8,9,10,1,4,3,2,7 };
        void printarray(int[]);
       

        cout << "Before:\n";
        printarray(a);


        quickSort(a, 0, 9);


        cout << "\nAfter:\n";
        printarray(a);

       
       

        return 0;
}


void printarray(int a[])
{

        for (int i = 0; i < 10; ++i)
        {
                cout.width(4);
                cout << a[i];

        }

}
JW2TTXZ)APAIHFW{8HM}3G9.png
TNDC1Z`J$C0DRI7SHN$JSP7.png
С¼×Óã×îÐÂ¿Î³Ì -> https://ilovefishc.com
 Â¥Ö÷| ·¢±íÓÚ 2020-3-29 14:24:14 | ÏÔʾȫ²¿Â¥²ã
¶¥Ò»ÏÂ
С¼×Óã×îÐÂ¿Î³Ì -> https://ilovefishc.com
 Â¥Ö÷| ·¢±íÓÚ 2020-3-30 13:10:26 | ÏÔʾȫ²¿Â¥²ã
ÔÙ¶¥Ò»ÏÂ
С¼×Óã×îÐÂ¿Î³Ì -> https://ilovefishc.com
·¢±íÓÚ 2020-3-30 16:09:53 | ÏÔʾȫ²¿Â¥²ã

»ØÌû½±Àø +2 Óã±Ò

¸Ð¾õswapº¯ÊýÓеã¶àÓà

  1. #include <stdio.h>

  2. int partition(int k[], int low, int high) {
  3.     int key = k[low];
  4.     while(low < high) {
  5.         while(low < high && key <= k[high])
  6.             --high;
  7.         k[low] = k[high];
  8.         while(low < high && key >= k[low])
  9.             ++low;
  10.         k[high] = k[low];
  11.     }
  12.     k[low] = key;
  13.     return low;
  14. }

  15. void qsort(int k[], int low, int high) {
  16.     if(low >= high) return;
  17.     int point = partition(k, low, high);
  18.     qsort(k, low, point - 1);
  19.     qsort(k, point + 1, high);
  20. }

  21. int main(void) {
  22.     int a[10] = {4, 2, 5, 0, 3, 9, 1, 7, 6, 8};
  23.     qsort(a, 0, 9);
  24.     for(int i = 0; i < 10; ++i) {
  25.         printf("%d ", a[i]);
  26.     }
  27.     printf("\n");
  28.     return 0;
  29. }
¸´ÖÆ´úÂë
С¼×Óã×îÐÂ¿Î³Ì -> https://ilovefishc.com
·¢±íÓÚ 2020-3-30 16:10:30 | ÏÔʾȫ²¿Â¥²ã
С¼×Óã×îÐÂ¿Î³Ì -> https://ilovefishc.com
ÄúÐèÒªµÇ¼ºó²Å¿ÉÒÔ»ØÌû µÇ¼ | Á¢¼´×¢²á

±¾°æ»ý·Ö¹æÔò

СºÚÎÝ|ÊÖ»ú°æ|Archiver|ÓãC¹¤×÷ÊÒ ( ÔÁICP±¸18085999ºÅ-1 | ÔÁ¹«Íø°²±¸ 44051102000585ºÅ)

GMT+8, 2025-7-1 05:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí