Language:
C++     Change language:
Pastebin: 95538
Author: ortiz
Subject: main.cpp
Created: 2008-09-07 16:50:15
Download and save
Toggle line numbers
1#include <iostream> 
2#include <fstream>  
3#include <ctime> 
4#include <string> 
5 
6using namespace std; 
7 
8int partition(int data[], int firstIdx, int lastIdx); 
9 
10int main () 
11 
12
13    int data[25]; 
14    int x = 0; 
15    int m = 0; 
16    int small = 0; 
17    int first = 0; 
18    int last = 0; 
19    int max = 25; 
20 
21    srand((unsigned)time(NULL)); 
22 
23    for (x=0;x<max;x++) //Sets array to 0 
24    { 
25        data[x] = 0; 
26    } 
27 
28            for (x=0;x<max;x++) // fills array with random numbers 
29            { 
30            data[x] = (rand() % 11); 
31            cout << "Slot = " << x << " Random # = " << data[x] << endl; 
32            } 
33 
34    first = data[0]; 
35    last = data[max-1]; 
36 
37    partition(data, first, last); // calls partition function 
38 
39    cout << "\n\nSORTED == \n" << endl; 
40 
41    for (x=0;x<max;x++) 
42 
43        cout << "Slot = " << x << " Random # = " << data[x] << endl; 
44 
45 
46 
47 
48cout << "DONE!!!! \n\n"
49 
50return 0; 
51 
52
53 
54 
55 
56Partition Function 
57------------------------------- 
58 
59#include <iostream> 
60using namespace std; 
61 
62int partition(int data[], int firstIdx, int lastIdx) 
63
64    int idx, smallIdx; 
65    int piv; 
66 
67    smallIdx = (firstIdx + lastIdx)/2; 
68    piv = data[smallIdx]; 
69 
70    // move pivot data to 1st position 
71    swap (data[firstIdx], data[smallIdx]); 
72    smallIdx = firstIdx; 
73 
74    for (idx = firstIdx+1; idx <= lastIdx; idx++) 
75    { 
76        if (data[idx] < piv) 
77        { 
78            smallIdx++; 
79            swap (data[idx], data[smallIdx]); 
80        } 
81    } 
82 
83    swap (data[firstIdx], data[smallIdx]); 
84 
85    return smallIdx; 
86
87 
88 
89 
90 
91 
92 
93 
Download and save
Toggle line numbers
Thread:
[95538] main.cpp by ortiz at 2008-09-07 16:50:15
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.