การใช้sortในภาษาซีคัยรู้มั่งค่ะ

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

nongsom
PHP Newbie
PHP Newbie
โพสต์: 1
ลงทะเบียนเมื่อ: 31/07/2009 9:22 pm

การใช้sortในภาษาซีคัยรู้มั่งค่ะ

โพสต์ที่ยังไม่ได้อ่าน โดย nongsom »

อยากรู้วิธีการใช้sortใครพอรู้ก็ช่วยบอกหน่อยนะค่ะ
wit163
PHP Newbie
PHP Newbie
โพสต์: 5
ลงทะเบียนเมื่อ: 04/02/2010 2:09 pm

Re: การใช้sortในภาษาซีคัยรู้มั่งค่ะ

โพสต์ที่ยังไม่ได้อ่าน โดย wit163 »

c หรือ cpp ครับ




ถ้า cpp ก็ใช้
std

ซึ่งเป็นแบบนี้ครับ


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;

// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33

// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)

// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}

แต่ถ้า
c
ธรรมดาก็

void q_sort(int numbers[], int left, int right);
void quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}


void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;

l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}

int main()
{
int a[]={12,5,6,34,89,2,3,31,72};
quickSort(a, 9);

int i=0;
for(;i< 9 ;i++)
{

printf(" %d",a);
}
return 0;
}


ครับ
แก้ไขล่าสุดโดย mindphp เมื่อ 04/02/2010 5:31 pm, แก้ไขไปแล้ว 1 ครั้ง.
เหตุผล: ??????????????????????????????????????????????????????????????????????????????????????????????????
noJMe
PHP Newbie
PHP Newbie
โพสต์: 5
ลงทะเบียนเมื่อ: 18/03/2010 11:02 am

Re: การใช้sortในภาษาซีคัยรู้มั่งค่ะ

โพสต์ที่ยังไม่ได้อ่าน โดย noJMe »

มันก็ขึ้นอยู่ว่า คุณจะใช้ sort แบบไหนอ่าคับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 59