Abdulbasit عبدالباسط غرايبة
عدد الرسائل : 1668 الموقع : alhash-school.yolasite.com العمر : 40 العمل/الترفيه : فني مختبرات نقاط التميز مسابقات : 130 نقاط التميز : 16183 السٌّمعَة : 48 الاوسمة : الاوسمة2 : احترام قوانين المنتدى : الدولة : تاريخ التسجيل : 28/11/2008
| موضوع: heap sort in c++ الإثنين يونيو 08, 2009 3:42 am | |
| include <iostream> #include <fstream> #include <iomanip>
using namespace std;
class HeapSort { private: int list[10]; protected: void fixup(int, int, int); void buildheap(); public: void heapsort(); void store(int, int); void printHeap(); };
void HeapSort::fixup(int value, int start, int last) { int look; bool done; done = false; look = 2*start+1; while(look <= last && !done) { if(look < last) { if(list[look] < list[look+1]) look = look+1; } if(value > list[look]) done = true; else { list[start] = list[look]; start = look; look = 2*start+1; } } }
void HeapSort::buildheap() { int count; int item; for(count = (10/2)-1; count >= 0; count--) { item = list[count]; fixup(item, count, 9); } }
void HeapSort::heapsort() { int count; int item; buildheap(); for(count = 9; count >= 1; count--) { //cout << list[count] << endl; //cout <<"count " << count << endl; item = list[count]; list[count] = list[0]; fixup(item, 0, count-1); //cout << list[count] << endl; } }
void HeapSort::store(int number, int place) { list[place] = number; }
void HeapSort::printHeap() { for(int i = 0; i < 10; i++) cout << list[i] << endl; } int main() { HeapSort heap1; HeapSort heap2;
int x = 0; int numberInput; cout << "Enter ten numbers: " << endl; while(x < 10) { cin >> numberInput; http://heap1.heapsort(); //cout << x; heap1.store(numberInput, x); x++; } x = 0; /*while(x < 10) { cin >> numberInput; heap2.store(numberInput, x); x++; } */ heap1.heapsort(); heap1.printHeap(); http://heap2.printHeap(); // heap2.heapsort(); http://heap2.printHeap(); system("pause"); }#include <iostream> #include <fstream> #include <iomanip>
using namespace std;
class HeapSort { private: int list[10]; protected: void fixup(int, int, int); void buildheap(); public: void heapsort(); void store(int, int); void printHeap(); };
void HeapSort::fixup(int value, int start, int last) { int look; bool done; done = false; look = 2*start+1; while(look <= last && !done) { if(look < last) { if(list[look] < list[look+1]) look = look+1; } if(value > list[look]) done = true; else { list[start] = list[look]; start = look; look = 2*start+1; } } }
void HeapSort::buildheap() { int count; int item; for(count = (10/2)-1; count >= 0; count--) { item = list[count]; fixup(item, count, 9); } }
void HeapSort::heapsort() { int count; int item; buildheap(); for(count = 9; count >= 1; count--) { //cout << list[count] << endl; //cout <<"count " << count << endl; item = list[count]; list[count] = list[0]; fixup(item, 0, count-1); //cout << list[count] << endl; } }
void HeapSort::store(int number, int place) { list[place] = number; }
void HeapSort::printHeap() { for(int i = 0; i < 10; i++) cout << list[i] << endl; } int main() { HeapSort heap1; HeapSort heap2;
int x = 0; int numberInput; cout << "Enter ten numbers: " << endl; while(x < 10) { cin >> numberInput; http://heap1.heapsort(); //cout << x; heap1.store(numberInput, x); x++; } x = 0; /*while(x < 10) { cin >> numberInput; heap2.store(numberInput, x); x++; } */ heap1.heapsort(); heap1.printHeap(); http://heap2.printHeap(); // heap2.heapsort(); http://heap2.printHeap(); system("pause"); } | |
|
Abdulbasit عبدالباسط غرايبة
عدد الرسائل : 1668 الموقع : alhash-school.yolasite.com العمر : 40 العمل/الترفيه : فني مختبرات نقاط التميز مسابقات : 130 نقاط التميز : 16183 السٌّمعَة : 48 الاوسمة : الاوسمة2 : احترام قوانين المنتدى : الدولة : تاريخ التسجيل : 28/11/2008
| موضوع: رد: heap sort in c++ الإثنين يونيو 08, 2009 3:46 am | |
| #include <stdlib.h> #include <stdio.h> #define NUM_ITEMS 100 void heapSort(int numbers[], int array_size); void siftDown(int numbers[], int root, int bottom); int numbers[NUM_ITEMS];
int main() { int i; //seed random number generator srand(getpid()); //fill array with random integers for (i = 0; i < NUM_ITEMS; i++) numbers[i] = rand(); //perform heap sort on array heapSort(numbers, NUM_ITEMS); printf("Done with sort.\n"); for (i = 0; i < NUM_ITEMS; i++) printf("%i\n", numbers[i]); }
void heapSort(int numbers[], int array_size) { int i, temp; for (i = (array_size / 2)-1; i >= 0; i--) siftDown(numbers, i, array_size); for (i = array_size-1; i >= 1; i--) { temp = numbers[0]; numbers[0] = numbers[i]; numbers[i] = temp; siftDown(numbers, 0, i-1); } }
void siftDown(int numbers[], int root, int bottom) { int done, maxChild, temp; done = 0; while ((root*2 <= bottom) && (!done)) { if (root*2 == bottom) maxChild = root * 2; else if (numbers[root * 2] > numbers[root * 2 + 1]) maxChild = root * 2; else maxChild = root * 2 + 1; if (numbers[root] < numbers[maxChild]) { temp = numbers[root]; numbers[root] = numbers[maxChild]; numbers[maxChild] = temp; root = maxChild; } else done = 1; } } | |
|
zgol عضو خبير
عدد الرسائل : 557 الموقع : alhash-shool.yoo7.com العمر : 28 العمل/الترفيه : طالب المزاج : من الاخر نقاط التميز : 7162 السٌّمعَة : 0 الاوسمة : احترام قوانين المنتدى : الدولة : تاريخ التسجيل : 29/03/2009
| موضوع: رد: heap sort in c++ الإثنين يوليو 06, 2009 9:34 pm | |
| | |
|