Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need help to make my code smaller. It works fine but its too long. Can someone help me to make it smaller? Outcome

Hi, I need help to make my code smaller. It works fine but its too long. Can someone help me to make it smaller? Outcome is shown below.image text in transcribed

File required for the code: https://drive.google.com/open?id=1_sE_auYSRsilqWa9GL00hCg-CRPQnlRI

#include

#include

#include

#include

#include

#include

using namespace std;

int MAX_VAL = 65536;

void move(int *arr, int dest, int source)

{

arr[dest] = arr[source];

}

void swap(int *arr, int a, int b)

{

int temp = arr[a];

arr[a] = arr[b];

arr[b] = temp;

}

/**

* randomArray - creates an array of randomly generated integers

* with the specified number of elements and the specified

* maximum value

*/

int *randomArray(int n, int maxVal, int minVal = 0)

{

int *arr = (int *)malloc(n*sizeof(int));

for (int i = 0; i

{

arr[i] = rand()%100;

}

return arr;

}

/** insertionSort */

void insertionSort(int *arr, int n)

{

//with benchmarks or timestamps to measure their running times

long double sysTime = time(0);

long double t1 = sysTime*1000;

for (int i = 1; i

{

if ((arr[i]

{

int toInsert = arr[i];

int j = i;

do

{

move(arr, j, j - 1);

j = j - 1;

}

while (j > 0 && (toInsert

arr[j] = toInsert;

}

}

long double sysTime2 = time(0);

long double t2 = sysTime2*1000;

cout

}

// Merge sort

// here p = low and q = high

void Merge(int *a, int p, int q, int mid)

{

// We have low to mid and mid+1 to high already sorted.

int i, j, k, temp[q-p+1];

i = p;

k = 0;

j = mid + 1;

// Merge the two parts into temp[].

while (i

{

if (a[i]

{

temp[k] = a[i];

k++;

i++;

}

else

{

temp[k] = a[j];

k++;

j++;

}

}

// Insert all the remaining values from i to mid into temp[].

while (i

{

temp[k] = a[i];

k++;

i++;

}

// Insert all the remaining values from j to high into temp[].

while (j

{

temp[k] = a[j];

k++;

j++;

}

// Assign sorted data stored in temp[] to a[].

for (i = p; i

{

a[i] = temp[i-p];

}

}

void MergeSort(int *a, int p, int q)

{

int mid;

if (p

{

mid=(p+q)/2;

// Split the data into two half.

MergeSort(a, p, mid);

MergeSort(a, mid+1, q);

// Merge them to get sorted output.

Merge(a, p, q, mid);

}

}

//Quick Sort

int partition (int A[], int p, int q)

{

int pivot = A[q]; // pivot

int i = (p - 1); // Index of smaller element

for (int j = p; j

{

// If current element is smaller than or

// equal to pivot

if (A[j]

{

i++; // increment index of smaller element

// swapping A[i] and A[j]

int temp = A[i];

A[i] = A[j];

A[j] = temp;

}

}

//swap(&A[i + 1], &A[q]);

int temp = A[i+1];

A[i+1] = A[q];

A[q]= temp;

return (i + 1);

}

/* The main function that implements QuickSort

A[] --> Array to be sorted,

p --> Starting index,

q --> Ending index */

void quickSort(int A[], int p, int q)

{

if (p

{

/* pi is partitioning index, A[p] is now

at right place */

int pi = partition(A, p, q);

// Separately sort elements before

// partition and after partition

quickSort(A, p, pi - 1);

quickSort(A, pi + 1, q);

}

}

int RandomizedQuickSortPartition(int low,int high,int a[])

{

srand(time(NULL));

int p=sizeof(a)/sizeof(a[0]);

int index=rand()%p+0;

//COMMENT 1 cout

int temp1=a[index];

a[index]=a[low];

a[low]=temp1;

int left,right,pivot_item=a[low];

left=low;

right=high;

while(left

{

while(a[left]

left++;

while(a[right]>pivot_item)

right--;

if(left

{

int temp=a[right];

a[right]=a[left];

a[left]=temp;

}

}

a[low]=a[right];

a[right]=pivot_item;

return right;

}

void RandomizedQuickSort(int low,int high,int a[])

{

int pivot;

if(low

{

pivot=RandomizedQuickSortPartition(low,high,a);

RandomizedQuickSort(low,pivot-1,a);

RandomizedQuickSort(pivot+1,high,a);

}

}

void printArray(int *arr, int n)

{

for (int i = 0; i

printf("%d ", arr[i]);

}

int main()

{

fstream file("unsorted6.txt");

srand(time(NULL));

int n;

char ch;

while(file >> n)

{

file >> ch;

int a[1000];

cout

for(int i=0; i

{

file >> a[i];

}

printArray(a, n);

cout

insertionSort(a, n);

printArray(a, n);

//with benchmarks or timestamps to measure their running times

long double sysTime = time(0);

long double t1 = sysTime*1000;

MergeSort(a, 0, n-1);

long double sysTime2 = time(0);

long double t2 = sysTime2*1000;

cout

printArray(a, n);

//with benchmarks or timestamps to measure their running times

long double qsysTime = time(0);

long double qt1 = qsysTime*1000;

quickSort(a, 0, n-1);

long double qsysTime2 = time(0);

long double qt2 = qsysTime2*1000;

cout

printArray(a, n);

//with benchmarks or timestamps to measure their running times

long double rsysTime = time(0);

long double rt1 = rsysTime*1000;

RandomizedQuickSort(0, n, a);

long double rsysTime2 = time(0);

long double rt2 = rsysTime2*1000;

cout

printArray(a, n);

free(a); // use this free function to free memory for array a

}

int toPause;

cin >> toPause;

return 0;

}

i Terminal Shell Edit View Window Help 100% E Sat Mar 17 9:59 AM E Marl 3--bash?201x 54 77 62 320 27 266 264 122 212 476 185 324 383 216 198 491 199 418 131 328 472 114 268 144 361 84 188 133 285 432 178 244 423 222 454 167 2e4 182 56 132 298 19 499 413 172 456 392 241 412 389 181 319 268 E 13e 65 289 58 429 298 195 139 488 247 176 72 394 2? 462 339 121 486 368 42 23 32 1B6 125 182 445 as 22 452 161 278 119 188 295 314 382 447 77 343 249 449 414 370 386 359 399 225 396 419 46? 344 349 485 3 467 174 ?? 398 479 161 489 498 489 91 12 443 69 147 398 97 483 311 263 497 498 211 113 416 276 484 2 129 171 238 255 435 292 221 25?464 426 53 389 333 135 469 312 36 494 ??? 256 14 495 431 159 213 187 184 421 59 492 92 4 61 297 334 31 299 336 364 46 284 153 386 217 141 103 25 441 438 47e 265 451 193 195 281 267 177 128 254 45B 148 219 7 422 261 290 346 166 323 365 286 31e 385 389 433 481 445 136 371 236 4 442 189 288 41 110 117 54 248 B 169 88 436 35 118 496 164 63 137 33 insertianSort Running time of insertion sort is 8 miliseconds 12345?7891011 12 13 14 15 16 17 18 19 28 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 3? 37 38 39 40 41 42 43 44 45 46 47 48 49 5e 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 ?7 a 69 70 B 129 138 131 132 133 134 135 136 137 138 139 148 141 142 143 144 145 146 147 148 149 158 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 188 181 182 183 184 185 186 187 188 189 198 191 192 193 194 195 196 197 198 199 288 281 282 283 284 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 238 231 232 233 234 235 236 237 238 239 248 241 242 243 244 245 246 247 248 249 258 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 2 79 288 281 282 283 284 285 286 287 288 289 298 291 292 293 294 295 296 297 298 299 380 301 382 383 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 32 9 338 331 332 333 334 335 336 337 338 339 348 341 342 343 344 345 346 347 348 349 358 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 388 381 382 383 384 385 386 387 388 389 398 391 392 393 394 395 396 397 398 399 400 401 482 483 484 495 486 487 408 489 419 411 412 413 414 415 416 417 418 419 42? 421 422 423 424 425 426 427 428 429 438 431 432 433 434 435 436 437 438 437 448 441 442 443 444 445 446 447 44844 458 451 452 453 454 455 456 457 458 45 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 4794 88 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 59 Running tine of nerge sort is 8 niliscconds 1 2 3 4567 89 18 11 12 13 14 15 16 17 18 19 28 21 22 23 24 25 26 27 28 29 38 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 4B 49 5e 51 52 53 54 55 56 57 58 59 68 61 62 63 64 65 66 67 68 69 70 B 129 13e 131 132 133 ?34 ?35 136 137 138 139 148 141 142 143 144 145 146 247 148 149 1se 151 152 153 1S4 1SS 156 ?57 158 159 168 161 162 163 164 165 166 167 268 169 170 171 172 173 174 175 176 177 178 179 188 181 182 183 184 285 186 187 1BB 1B9 198 191 192 193 194 195 196 197 198 199 2ee 2B1 282 283 284 29S 286 267 288 289 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 238 231 232 233 234 235 236 237 238 239 24e 241 242 243 244 245 246 247 24B 249 25e 251 252 253 254 2SS 256 257 258 259 260 261 262 263 244 265 266 267 268 269 270 271 272 273 274 275 276 277 278 2 79 288 281 282 283 284 28S 2B6 287 2BB 2B9 29 291 292 293 294 295 296 297 29B 299 300 381 3e2 303 34 395 306 307 30B 309 318 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 324 327 328 32 9 338 331 332 333 334 335 336 337 338 339 34e 341 342 343 344 345 346 347 348 349 358 351 352 353 354 355 356 357 358 359 368 361 362 363 364 345 346 367 368 369 37 372 373 374 375 37 377 378 379 383 384 385 386 387 388 389 398 391 392 393 394 395 396 397 398 399 460 481 402 483 484 495 496 487 408 409 410 411 412 413 414 415 414 417 418 419 420 421 422 423 424 425 426 427 428 429 280 381 43e 431 432 433 434 436 436 437 438 439 448 441 442 443 444 445 446 447 448 449 460 451 452 453 454 455 456 457 458 459 460 461 462 463 464 445 446 467 468 469 470 471 472 473 474 475 476 477 478 479 4 Be 481 482 483 484 486 486 487 488 489 498 491 492 493 494 495 496 497 498 499 666 Running time of quick sort is 0 miliseconds 1 2 34 5 6 7 89 19 11 12 13 14 16 16 17 18 19 22 21 22 23 24 26 26 27 28 29 30 31 32 33 34 71 72 73 74 75 76 77 78 79 80 81 ? 129 37 38 47 48 49 58 51 52 53 67 68 69 60 63 64 65 67 68 69 70 86 87 88 99 91 92 194 195 196 133 134 135 136 137 138 139 14 141 142 143 144 145 146 147 148 149 15e 151 152 153 154 155 156 158 65 166 167 168 169 188 288 300 334 335 336 337 338 339 348 341 342 343 344 345 346 347 348 349 358 351 352 353 35 384 385 386 387 388 389 39e 363 364 365 366 367 368 369 370 371 372 37 375 388 39P 408 484 405 406 08 409 458 459 460 433 434 442 443 444 445 446 453 454 462 463 464 465 466 468 469 482 483 484 488 48 95 496 andonized quic seconds crror reed was not 22 23 24 25 28 29 3e 33 34 35 36 37 38 39 46 47 48 S2 53 54 55 5B 59 68 61 62 63 66 67 68 80 B1 B2 B3 84 85 86 87 88 B 99 1ee 144 145 164 165 166 84 1B5 186 188 189 205 206 20B 209 2B8 289 29e 294 295 296 298 299 3ee 394 305 306 307 30B 309 317 318 48 341 343 344 345 346 368 388 382 383 384 386 386 387 388 408 409 468 494 405 406 414 415 416 417 448 441 442 443 444 446 446 s-Nae8ook i Terminal Shell Edit View Window Help 100% E Sat Mar 17 9:59 AM E Marl 3--bash?201x 54 77 62 320 27 266 264 122 212 476 185 324 383 216 198 491 199 418 131 328 472 114 268 144 361 84 188 133 285 432 178 244 423 222 454 167 2e4 182 56 132 298 19 499 413 172 456 392 241 412 389 181 319 268 E 13e 65 289 58 429 298 195 139 488 247 176 72 394 2? 462 339 121 486 368 42 23 32 1B6 125 182 445 as 22 452 161 278 119 188 295 314 382 447 77 343 249 449 414 370 386 359 399 225 396 419 46? 344 349 485 3 467 174 ?? 398 479 161 489 498 489 91 12 443 69 147 398 97 483 311 263 497 498 211 113 416 276 484 2 129 171 238 255 435 292 221 25?464 426 53 389 333 135 469 312 36 494 ??? 256 14 495 431 159 213 187 184 421 59 492 92 4 61 297 334 31 299 336 364 46 284 153 386 217 141 103 25 441 438 47e 265 451 193 195 281 267 177 128 254 45B 148 219 7 422 261 290 346 166 323 365 286 31e 385 389 433 481 445 136 371 236 4 442 189 288 41 110 117 54 248 B 169 88 436 35 118 496 164 63 137 33 insertianSort Running time of insertion sort is 8 miliseconds 12345?7891011 12 13 14 15 16 17 18 19 28 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 3? 37 38 39 40 41 42 43 44 45 46 47 48 49 5e 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 ?7 a 69 70 B 129 138 131 132 133 134 135 136 137 138 139 148 141 142 143 144 145 146 147 148 149 158 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 188 181 182 183 184 185 186 187 188 189 198 191 192 193 194 195 196 197 198 199 288 281 282 283 284 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 238 231 232 233 234 235 236 237 238 239 248 241 242 243 244 245 246 247 248 249 258 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 2 79 288 281 282 283 284 285 286 287 288 289 298 291 292 293 294 295 296 297 298 299 380 301 382 383 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 32 9 338 331 332 333 334 335 336 337 338 339 348 341 342 343 344 345 346 347 348 349 358 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 388 381 382 383 384 385 386 387 388 389 398 391 392 393 394 395 396 397 398 399 400 401 482 483 484 495 486 487 408 489 419 411 412 413 414 415 416 417 418 419 42? 421 422 423 424 425 426 427 428 429 438 431 432 433 434 435 436 437 438 437 448 441 442 443 444 445 446 447 44844 458 451 452 453 454 455 456 457 458 45 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 4794 88 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 59 Running tine of nerge sort is 8 niliscconds 1 2 3 4567 89 18 11 12 13 14 15 16 17 18 19 28 21 22 23 24 25 26 27 28 29 38 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 4B 49 5e 51 52 53 54 55 56 57 58 59 68 61 62 63 64 65 66 67 68 69 70 B 129 13e 131 132 133 ?34 ?35 136 137 138 139 148 141 142 143 144 145 146 247 148 149 1se 151 152 153 1S4 1SS 156 ?57 158 159 168 161 162 163 164 165 166 167 268 169 170 171 172 173 174 175 176 177 178 179 188 181 182 183 184 285 186 187 1BB 1B9 198 191 192 193 194 195 196 197 198 199 2ee 2B1 282 283 284 29S 286 267 288 289 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 238 231 232 233 234 235 236 237 238 239 24e 241 242 243 244 245 246 247 24B 249 25e 251 252 253 254 2SS 256 257 258 259 260 261 262 263 244 265 266 267 268 269 270 271 272 273 274 275 276 277 278 2 79 288 281 282 283 284 28S 2B6 287 2BB 2B9 29 291 292 293 294 295 296 297 29B 299 300 381 3e2 303 34 395 306 307 30B 309 318 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 324 327 328 32 9 338 331 332 333 334 335 336 337 338 339 34e 341 342 343 344 345 346 347 348 349 358 351 352 353 354 355 356 357 358 359 368 361 362 363 364 345 346 367 368 369 37 372 373 374 375 37 377 378 379 383 384 385 386 387 388 389 398 391 392 393 394 395 396 397 398 399 460 481 402 483 484 495 496 487 408 409 410 411 412 413 414 415 414 417 418 419 420 421 422 423 424 425 426 427 428 429 280 381 43e 431 432 433 434 436 436 437 438 439 448 441 442 443 444 445 446 447 448 449 460 451 452 453 454 455 456 457 458 459 460 461 462 463 464 445 446 467 468 469 470 471 472 473 474 475 476 477 478 479 4 Be 481 482 483 484 486 486 487 488 489 498 491 492 493 494 495 496 497 498 499 666 Running time of quick sort is 0 miliseconds 1 2 34 5 6 7 89 19 11 12 13 14 16 16 17 18 19 22 21 22 23 24 26 26 27 28 29 30 31 32 33 34 71 72 73 74 75 76 77 78 79 80 81 ? 129 37 38 47 48 49 58 51 52 53 67 68 69 60 63 64 65 67 68 69 70 86 87 88 99 91 92 194 195 196 133 134 135 136 137 138 139 14 141 142 143 144 145 146 147 148 149 15e 151 152 153 154 155 156 158 65 166 167 168 169 188 288 300 334 335 336 337 338 339 348 341 342 343 344 345 346 347 348 349 358 351 352 353 35 384 385 386 387 388 389 39e 363 364 365 366 367 368 369 370 371 372 37 375 388 39P 408 484 405 406 08 409 458 459 460 433 434 442 443 444 445 446 453 454 462 463 464 465 466 468 469 482 483 484 488 48 95 496 andonized quic seconds crror reed was not 22 23 24 25 28 29 3e 33 34 35 36 37 38 39 46 47 48 S2 53 54 55 5B 59 68 61 62 63 66 67 68 80 B1 B2 B3 84 85 86 87 88 B 99 1ee 144 145 164 165 166 84 1B5 186 188 189 205 206 20B 209 2B8 289 29e 294 295 296 298 299 3ee 394 305 306 307 30B 309 317 318 48 341 343 344 345 346 368 388 382 383 384 386 386 387 388 408 409 468 494 405 406 414 415 416 417 448 441 442 443 444 446 446 s-Nae8ook<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions