Docsity

Sorting Algorithms: Bubble Sort, Selection Sort, Insertion Sort, and Array Manipulation, Slides of Computer Science

Partial preview of the text

Download Sorting Algorithms: Bubble Sort, Selection Sort, Insertion Sort, and Array Manipulation and more Slides Computer Science in PDF only on Docsity!

Some Sorting Algorithms

Bubble Sort Let X be an array. In the last exercise we looked at

the loop structure: for(j=0; j<X.Length-1; j++)

if(X[j]>X[j+1])

MySwap(ref X[j],ref X[j+1]);

It was discovered that when executed, this loop moved

the largest element to the last position in the array.

for(i=1; i<=X.Length-1; i++)

for(j=0; j<X.Length-1; j++)

if(X[j]>X[j+1])

MySwap(ref X[j],ref

X[j+1]);

The assumption here is that MySwap is a

declared method that will swap element X[j]with X[j+1].

Note that once the largest element is moved to

the end, there really is no need to everconsider it again. So we can make thefollowing change. for(i=1; i<=X.Length-1; i++)

for(j=0; j<X.Length-

i

; j++)

if(X[j]>X[j+1])

MySwap(ref X[j],ref

X[j+1]);

Adding all these up, we get(N-1) + (N-2) + (N-3) + … + 1 and this is equal

to (N-1)*N/2 which is N

2 /2 - N/2.

As N gets larger and larger, how does N

compare to N/2?

Selection Sort Method: If N is the length of the array set some

index variable i = N-1, the last index of thearray. Find the index of the largest valuefrom X[0] to X[i], and put that index in somevariable k. Swap X[i] and X[k]. Now reduce i by one and repeat. Keep doing it

for all i from N-1 to 1. We’ll look at the moreformal algorithm in class.

Insertion Sort (Algorithm) This sort is inductive. Suppose you are trying to

sort an array of length N. If you know thatelements in X[0] through X[k] are already inincreasing order, you can extend it to X[k+1]by saving X[k+1] and then moving thoseelements on the left that are larger thanX[k+1] one place to the right. Then insert thesaved element into the resulting “hole” in thearray.

Example:

10

15

20

13

17

5

11

The first element not in increasing order is 13 which is

in X[3]. So we save it. Then note that 20 is greaterso it moves one to the right and 15 is greater and itmoves one to the right. 10 is not greater. So with themoving we get:

10

15

15

20

17

5

11

Then we put 13 in the place where we stopped, to get:

10

13

15

20

17

5

11,

and the array is sorted to one more place.

for(i=1; i<=X.Length-1; i++){

key = X[i];j = i-1; /start one to the leftwhile(j>=0 && X[j]>key){

X[j+1]=X[j];j--;

} X[j+1]=key; }

Cost of Insertion Sort The cost here is not quite as straight forward as

the previous two, since the cost depends onthe initial condition of the array. We will dothis in class, and see that it can be as bad assome constant times N

2

but as good as some

constant times N.

Multi-subscripted Arrays We will restrict ourselves to “two” for “multi”.There are two kinds of double subscripted

arrays in C#. 

Rectangular 

Jagged

Rectangular All the rows have the same number of

elements, and all the columns have the samenumber of elements; although, the columnand row lengths may be different. Declaring a rectangular array:

int[,] X;X = new int[15,20]; X is now a rectangular array with 15 rows and

20 columns. X is an object.

Passing to a Method Passing a rectangular array to a method is much like

passing a singly subscripted array. Problem: Write a method that will receive a

rectangular array and initialize it so that all theelements on row k have value k. In solving such aproblem, we will make use of nested for loops,where the outer loop controls the row number andthe inner loop controls the column number. Youmust become familiar with this technique.

static

void Initialize(int[,] X)

{

int

i, j;

for

(i = 0;

i < X.GetLength(0);i++)

{

for (j =

0; j <

X.GetLength(1); j++)

X[i,j]

= i;

}

}