Docsity

Sorting, Mean, Median and Mode Calculation and Searching Techniques in Arrays, Slides of Computer Programming

Partial preview of the text

Download Sorting, Mean, Median and Mode Calculation and Searching Techniques in Arrays and more Slides Computer Programming in PDF only on Docsity!

Computer Programming^ • Sorting Arrays

docsity.com

Sorting Arrays^ ļ‚—^ Sorting data^ ā—¦^ Important computing application^ ā—¦^ Virtually every organization must sort some data^ ļ‚–^ Massive amounts must be sorted^ ļ‚—^ Bubble sort (sinking sort)^ ā—¦^ Several passes through the array^ ā—¦^ Successive pairs of elements are compared^ ļ‚–^ If increasing order (or identical), no change^ ļ‚–^ If decreasing order, elements exchanged^ ā—¦^ Repeat these steps for every element

docsity.com

Sorting Arrays^ ļ‚—^ Swapping variables^ int^ x^ =^ 3,

y^ =^ 4;y = x;x = y; ļ‚— What happened? ā—¦ Both x and y are 3! ā—¦ Need a temporary variable ļ‚— Solution int x = 3, y^ =^ 4,^ temp^ =

0; temp^ =^ x;^ //^

temp^ gets^3 x^ =^ y;^ //^

x^ gets^4 y^ =^ temp;^ //^

y^ gets^3

docsity.com

1 //^ code^14 2 //^ This^ program^

sorts^ an^ array's^ values

into^ ascending^ order. 3 #include^ 4 5 using^ std::cout; 6 using^ std::endl; 7 8 #include^ 9 10 using^ std::setw; 1112 int^ main() 13 { 14 const^ int^ arraySize

=^ 10;^ //^ size^ of^ array^ a 15 int^ a[^ arraySize

]^ =^ {^ 2,^ 6,^ 4,^ 8,^

10,^ 12,^ 89,^ 68,^ 45,

16 int^ hold;^ //

temporary^ location used^ to^ swap^ array elements

1718 cout^ <<^ "Data

items^ in^ original^ order\n"; 1920 //^ output^ original

array 21 for^ (^ int^ i^ =

0;^ i^ <^ arraySize;^ i++^ ) 22 cout^ <<^ setw(

4 )^ <<^ a[^ i^ ]; 23

docsity.com

40 cout^ <<^ "\nData

items^ in^ ascending^

order\n";

4142 //^ output^ sorted

array 43 for^ (^ int^ k^ =

0;^ k^ <^ arraySize;^ k++^ ) 44 cout^ <<^ setw(

4 )^ <<^ a[^ k^ ]; 4546 cout^ <<^ endl; 4748 return^ 0;^ //

indicates^ successful

termination

4950 }^ //^ end^ main Data^ items^ in^ original

order

Data^ items^ in^ ascending

order

docsity.com

Reading Assignment: Computing Mean,Median and Mode Using Arrays^ ļ‚—^ Mean^ ā—¦^ Average (sum/number of elements)^ ļ‚—^ Median^ ā—¦^ Number in middle of sorted list^ ā—¦^ 1, 2, 3, 4, 5 (3 is median)^ ā—¦^ If even number of elements, take average ofmiddle two^ ļ‚—^ Mode^ ā—¦^ Number that occurs most often^ ā—¦^ 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)

docsity.com

26 int^ frequency[

10 ]^ =^ {^0 };^ //^

initialize^ array^ frequency

2728 //^ initialize

array^ responses 29 int^ response[

responseSize^ ]^ = 30 {^ 6,^ 7,

8,^ 9,^ 8,^ 7,^ 8,^ 9,^

8,^ 9,

31 7,^ 8,

9,^ 5,^ 9,^ 8,^ 7,^ 8,^

7,^ 8,

32 6,^ 7,

8,^ 9,^ 3,^ 9,^ 8,^ 7,^

8,^ 7,

33 7,^ 8,

9,^ 8,^ 9,^ 8,^ 9,^ 7,^

8,^ 9,

34 6,^ 7,

8,^ 7,^ 8,^ 7,^ 9,^ 8,^

9,^ 2,

35 7,^ 8,

9,^ 8,^ 9,^ 8,^ 9,^ 7,^

5,^ 3,

36 5,^ 6,

7,^ 2,^ 5,^ 3,^ 9,^ 4,^

6,^ 4,

37 7,^ 8,

9,^ 6,^ 8,^ 7,^ 8,^ 9,^

7,^ 8,

38 7,^ 4,

4,^ 2,^ 5,^ 3,^ 8,^ 7,^

5,^ 6,

39 4,^ 5,

6,^ 1,^ 6,^ 5,^ 7,^ 8,^

4041 //^ process^ responses 42 mean(^ response,

responseSize^ ); 43 median(^ response,

responseSize^ ); 444546 return^ 0;^ //

indicates^ successful

termination 4748 }^ //^ end^ main 49

docsity.com

50 //^ calculate^ average

of^ all^ response^ values 51 void^ mean(^ const

int^ answer[],^ int^ arraySize^ ) 52 { 53 int^ total^ =^ 0; 5455 cout^ <<^ "********\n

Mean\n********\n"; 5657 //^ total^ response

values 58 for^ (^ int^ i^ =

0;^ i^ <^ arraySize;^ i++^ ) 59 total^ +=^ answer[

i^ ]; 6061 //^ format^ and

output^ results 62 cout^ <<^ fixed

<<^ setprecision(^4

6364 cout^ <<^ "The^

mean^ is^ the^ average value^ of^ the^ data\n" 65 <<^ "items.

The^ mean^ is^ equal^ to^ the^ total^ of\n" 66 <<^ "all^

the^ data^ items^ divided

by^ the^ number\n" 67 <<^ "of^ data

items^ ("^ <<^ arraySize 68 <<^ ").^ The

mean^ value^ for\nthis

run^ is:^ " 69 <<^ total

<<^ "^ /^ "^ <<^ arraySize

<<^ "^ =^ "

70 <<^ static_cast<

double^ >(^ total^ )^ /^ arraySize 71 <<^ "\n\n"; 7273 }^ //^ end^ function

mean 74

We cast to a double to getdecimal points for theaverage (instead of aninteger).

docsity.com

144145 //^ function^ that

sorts^ an^ array^ with

bubble^ sort^ algorithm 146 void^ bubbleSort(

int^ a[],^ int^ size^

147 { 148 int^ hold;^ //

temporary^ location^

used^ to^ swap^ elements 149150 //^ loop^ to^ control

number^ of^ passes 151 for^ (^ int^ pass

=^ 1;^ pass^ <^ size;^ pass++^ ) 152153 //^ loop^ to

control^ number^ of^ comparisons^ per^ pass 154 for^ (^ int^

j^ =^ 0;^ j^ <^ size^ -^ 1;

j++^ ) 155156 //^ swap

elements^ if^ out^ of order 157 if^ (^ a[

j^ ]^ >^ a[^ j^ +^1 ]^ )^

158 hold

=^ a[^ j^ ]; 159 a[^ j

]^ =^ a[^ j^ +^1 ]; 160 a[^ j

+^1 ]^ =^ hold; 161162 }^ //^ end

if 163164 }^ //^ end^ function

bubbleSort 165

docsity.com

166 //^ output^ array^

contents^ (20^ values^

per^ row)

167 void^ printArray(

const^ int^ a[],^ int size^ ) 168 { 169 for^ (^ int^ i^ =

0;^ i^ <^ size;^ i++^ )^

170171 if^ (^ i^ %^20

==^0 )^ //^ begin^ new

line^ every^20 values 172 cout^ <<

endl; 173174 cout^ <<^ setw(

2 )^ <<^ a[^ i^ ]; 175176 }^ //^ end^ for 177178 }^ //^ end^ function

printArray

docsity.com

********Mode********Response^ Frequency Histogram

The^ mode^ is^ the^ most

frequent^ value. For^ this^ run^ the^ mode

is^8 which^ occurred

27 times.

docsity.com

Searching Arrays: Linear Search andBinary Search^ ļ‚—^ Search array for a key value^ ļ‚—^ Linear search^ ā—¦^ Compare each element of array with keyvalue^ ļ‚–^ Start at one end, go to other^ ā—¦^ Useful for small and unsorted arrays^ ļ‚–^ Inefficient^ ļ‚–^ If search key not present, examines every element

docsity.com

1 //^ code^16 2 //^ Linear^ search

of^ an^ array. 3 #include^ 4 5 using^ std::cout; 6 using^ std::cin; 7 using^ std::endl; 8 9 int^ linearSearch(

const^ int^ [],^ int, int^ );^ //^ prototype 1011 int^ main() 12 { 13 const^ int^ arraySize

=^ 100;^ //^ size^ of array^ a 14 int^ a[^ arraySize

];^ //^ create

array^ a 15 int^ searchKey;

//^ value

to^ locate^ in^ a 1617 for^ (^ int^ i^ =

0;^ i^ <^ arraySize;^ i++^ )^ //^ create^ some

data

18 a[^ i^ ]^ =^2

*^ i; 1920 cout^ <<^ "Enter

integer^ search^ key:

21 cin^ >>^ searchKey; 2223 //^ attempt^ to

locate^ searchKey^ in

array^ a 24 int^ element^ =

linearSearch(^ a,^ searchKey,

arraySize^ ); 25

Takes array, search key,and array size.

docsity.com

26 //^ display^ results 27 if^ (^ element^

!=^ -1^ )

28 cout^ <<^ "Found

value^ in^ element^ " <<^ element^ <<^ endl; 29 else 30 cout^ <<^ "Value

not^ found"^ <<^ endl; 3132 return^ 0;^ //

indicates^ successful

termination 3334 }^ //^ end^ main 3536 //^ compare^ key^ to

every^ element^ of^ array

until^ location^ is 37 //^ found^ or^ until

end^ of^ array^ is^ reached;

return^ subscript^ of 38 //^ element^ if^ key

or^ -1^ if^ key^ not^ found 39 int^ linearSearch(

const^ int^ array[], int^ key,^ int^ sizeOfArray

40 { 41 for^ (^ int^ j^ =

0;^ j^ <^ sizeOfArray;

j++^ ) 4243 if^ (^ array[

j^ ]^ ==^ key^ )^ //^ if

found, 44 return^

j;^ //^ return^ location^ of^ key

4546 return^ -1;^ //

key^ not^ found 4748 }^ //^ end^ function

linearSearch

Enter^ integer^ search

key:^36

Found^ value^ in^ element

Enter^ integer^ search

key:^37

Value^ not^ found

docsity.com