1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/* My name */
/*
Based off of...
*******************************
Bubble sort
void bubbleSort()
{
// bubbles each element forward by comparing it to it's neighbor
// loops until finished
int temp;
int toSort[10] = {10,9,8,7,6,5,4,3,2,1};
for (int y=0; y < 10; y++)
{
for(int x=0; x < 10; x++)
{
if (toSort[x] > toSort[x+1])
{
temp = toSort[x];
toSort[x] = toSort[x+1];
toSort[x+1] = temp;
}
cout << toSort[x] << " ";
}
cout << endl;
}
}
Pseudocode is confusing!!
*******************************
*******************************
Selection sort
void selectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
int s = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[s]) {
s = j;
}
}
int t = a[i];
a[i] = a[s];
a[s] = t;
}
}
*******************************
*******************************
Insertion sort
insertionSort(array A)
begin
for i := 1 to length[A]-1 do
begin
value := A[i];
j := i-1;
while j = 0 and A[j] > value do
begin
A[j + 1] := A[j];
j := j-1;
end;
A[j+1] := value;
end;
end;
*******************************
*******************************
mode of array by Manjiri
#include<stdio.h>
int main()
{
int i,j,k=1,p,a[20],b[20],n,cnt=1,big;
printf("Enter the maximum number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
for(i=1; i<=n; i++)
{
for(j=i+1;j<=n; j++)
{
if(a[i]==a[j])
cnt++;
}
b[k]=cnt;
k++;
cnt=1;
}
big=b[1];
p=1;
for(i=2; i<=n; i++)
{
if(big<b[i])
{
big=b[i];
p=i;
}
}
printf("The element that occurs offenly is %d\n",a[p]);
printf("And it has occurred %d times\n",b[p]);
return 0;
}
*******************************
From Wikipedia
* Your notes
* http://en.wikipedia.org/wiki/Selection_sort
* http://en.wikipedia.org/wiki/Insertion_sort
* http://bytes.com/topic/c/answers/569655-how-find-mode-array
*/
#include <iostream>
#include <math.h>
using namespace std;
void mean(int [], int);
void median(int [], int);
void mode(int [], int);
void range(int [], int);
void stdDev(int [], int, int);
void bubble(int [], int);
void select(int [], int);
void insert(int [], int);
int main()
{
int howMany;
int counter=0;
char pickLett;
cout << "How many number do you want to input? ";
cin >> howMany;
int* Nums = new int[howMany]; // a pointer (i hope it works)
for(counter < howMany; counter++;) // my 1st for loop :D It's soo easy
Nums[counter] = (rand() % 100);
cout << "Do you want to use... \n";
cout << "(B)ubble sort\n";
cout << "(S)election sort\n";
cout << "(I)nsertion sort\n";
cout << "pick ";
cin >> pickLett;
switch(pickLett)
{
case 'b'||'B':
bubble(Nums[], howMany);
break;
case 's'||'S':
select(Nums[], howMany);
break;
case 'i'||'I':
insert(Nums[], howMany);
break;
default:
cout << "b or s or i\n";
break;
}
}
void mean(int Nums[], int howMany);
{
int daMean;
for ( int i = 0; i < howMany; i++ )
daMean += Nums[ i ];
cout << "The mean is " << daMean << "\n";
stdDev(Nums[], howMany, daMean);
}
void median(int Nums[], int howMany);
{
cout << int(Nums[howMany/2]);
}
void mode(int Nums[], int howMany);
{
int count=1, b[20], big, p;
for(int i=0;i<=howMany;i++;)
{
for(j=i+1; j<=howMany;j++)
{
if(Nums[i]==Nums[j])
count++;
}
b[k]=count;
k++;
count=1;
}
big=b[1]; // I wish Manjiri left comments or used more meaningful names
p=1;
for(int i=2;i<=howMany;i++;)
{
if(big<b[i])
{
big=b[i];
p=1;
}
}
cout << Nums[p] << " occured " << b[p] << " more than all the other numbers";
}
void range(int Nums[], int howMany);
{
cout << (Nums[0] - Nums[howMany-1]);
}
void stdDev(int Nums[], int howMany, int daMean);
{
//int* deviat = new int[howMany];
int daMeanDeviat
for(int i=0; i<howMany; i++) {
daMeanDeviat += (pow(Nums[i] - daMean),2);
}
cout << "The standard deviationis " << sqrt(daMeanDeviat);
}
/*Sorters below*/
void bubble(int Nums[], int howMany)
{
for(int i=0; i< howMany; i++;) {
for(int j=0; j < howMany; j++;) {
if(Nums[j] > Nums[j+1])
{
int tmp = Nums[j];
Nums[j] = Nums[j+1];
Nums[j+1] = tmp;
}
}
}
mean(Nums[], howMany);
median(Nums[], howMany);
mode(Nums[], howMany);
range(Nums[], howMany);
}
void select(int Nums[], int howMany)
{
for(int i=0; i<howMany; i++) {
int s = i;
for (int j = i + 1; j < howMany; j++) {
if (Nums[j] < Nums[s]) {
s = j;
}
}
int t = Nums[i];
Nums[i] = Nums[s];
Nums[s] = t;
}
mean(Nums[], howMany);
median(Nums[], howMany);
mode(Nums[], howMany);
range(Nums[], howMany);
}
void insert(int Nums[], int howMany)
{
for(int i=1; i<howMany; i++;) {
int tmp = Nums[i];
int j = i - 1;
while((j=0)&&(Nums[j]>tmp)) {
Nums[j+1] = Nums[j];
j = j - 1;
Nums[j+1] = tmp
}
}
mean(Nums[], howMany);
median(Nums[], howMany);
mode(Nums[], howMany);
range(Nums[], howMany);
}
|