Preview

Best Case Scenarios

Powerful Essays
Open Document
Open Document
442 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Best Case Scenarios
Best Case Scenario

cost times for(int i=0; i< arr[pos_min]) c4 n^2-1 pos_min = j; c5 0 if(pos_min != i) c6 n^2-1 temp=arr[i]; c7 0 arr[i]=arr[pos_min]; c8 0 arr[pos_min]=temp; c9 0

c1n + c2(n-1) + c3n^2 + c4(n^2-1) + c5(0) + c6(n^2-1) + c7(0) + c8(0) + c9(0) =
(c3 + c4 + c6)n^2 + (c1 + c2)n - (c2 + c4 + c6)
This is quadratic an^2 + bn + c

---------------------------
Worst Case Scenario

cost times for(int i=0; i< arr[pos_min]) c4 n^2-1 pos_min = j; c5 n^2-1 if(pos_min != i) c6 n^2-1 temp=arr[i]; c7 n^2-1 arr[i]=arr[pos_min]; c8 n^2-1 arr[pos_min]=temp; c9 n^2-1

c1n + c2(n-1) + c3n^2 + c4(n^2-1) + c5(n^2-1) + c6(n^2-1) + c7(n^2-1) + c8(n^2-1) + c9(n^2-1) =
(c3 + c4 + c5 + c6 + c7 + c8 + c9)n^2 + (c1 +
…show more content…
//Brady Lewis
//9-1-2015

#include

using namespace std;

//Define sorting function void selectSort(int arr[], int n)
{
//declare vars for current min and placeholder temp var int pos_min,temp;

//loop through entire array for (int i=0; i < n-1; i++) { pos_min = i;//set pos_min to the current index of array //loop through array starting with position after current index for (int j=i+1; j < n; j++) { //if value in next index is less than value in current index, set pos_min to next index if (arr[j] < arr[pos_min]) pos_min=j;

}

//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur if (pos_min != i) { temp = arr[i]; //set temp var to value of current index arr[i] = arr[pos_min]; //set current index to value of new pos_min arr[pos_min] = temp; //set pos_min to value of original current index } } //loop through entire array to output the sorted

You May Also Find These Documents Helpful