Title: Software 1: Practice 3
1Software 1 Practice 3
2Overview
- Random Numbers Generator
- rand()
- srand()
- dice
- Sorting algorithms
- InsertionSort
- QuickSort
3Random Numbers Generator
- rand and srand()
- Simulating the rolling of a pair of dice
4Rand()
int rand() - returns a pseudo-random integer
between 0 and RAND_MAX (32767).
- include ltstdio.hgt
- include ltstdlib.hgtint main() int
i for (i 0 i lt 5 i)
printf("d\n",rand() return
0
5Seed Definition
- Observation The results of several runs are
identical. - Explanation The initial seed used by rand() is
identical each time. - The Seed
- Used for the generation of the random numbers.
- Explicitly specified using the srand function.
6srand()
- void srand (unsigned int seed)
- Sets seed as the seed for a new sequence of
pseudo-random integers to be returned by rand(). - The sequences can be repeatable by calling
srand() with the same seed value. - The system time is a good choice as an argument
for srand. It will be different each time the
program is run. Thus, results will also be
different.
7srand()
- include ltstdio.hgt
- include ltstdlib.hgt
- include lttime.hgtint main() int i
- / Set the seed / srand( time( NULL )
) for (i 0 i lt 5 i) printf("
d\n",rand() return 0
srand(time(NULL)) is the same as int seed seed
time(NULL) srand(seed)
8- include ltstdio.hgt
- include ltstdlib.hgt
- int main ()
- int i
- int d1, d2
- int a13 / uses 2..12 /
- for (i 2 i lt 12 i) /
initialize / - ai 0
-
- for (i 0 i lt 1000 i) / Compute /
- d1 rand() 6 1
- d2 rand() 6 1
- ad1 d2 ad1 d2 1 / Count the
occurrences / -
- for (i 2 i lt 12 i) / Print /
- printf ("2d 3d\n", i, ai)
- return 0
ai counts how many times a pair of dice rolled
i.
rand() 6 produces random numbers in the range 0
to 5, rand() 6 1 produces random numbers in
the range 1 to 6.
9Output of dice with rand() and srand()
dice (with rand)
sdice(with srand)
10Sorting Algorithms
- main and swap
- InsertionSort
- QuickSort
11- include ltstdio.hgt
- include ltstdlib.hgt
- define BUFFER_SIZE 6
- int bufferBUFFER_SIZE
- int main(void)
- int i, j, current
- / reading input /
- for (i 0 i lt BUFFER_SIZE (scanf("d",
current) 1) i) - bufferi current
- if (i 0) return 0
- insertion_sort(i) / Later will be replaced
by quick_sort(0, i-1) / - / printing the sorted array /
- for (j 0 j lt i j)
- printf("d ", bufferj)
- printf("\n")
- return 0
-
Array of digits that will be sorted
Read the input digit
Store the digit in the next array location
Calling the sorting function that will sort the
global buffer.
12Swap
- void swap(int i, int j)
-
- int temp
- temp bufferi
- bufferi bufferj
- bufferj temp
bufferj
bufferi
13Insertion Sort
- void insertion_sort(int size)
-
- int j, k
- for (j 1 j lt size j)
- for (k j k gt 0 (bufferk lt
bufferk-1) --k) - swap(k, k-1)
14for (j 1 j lt size j) for (k j k gt 0
(bufferk lt bufferk-1) --k)
swap(k, k-1)
J1, k 1 swap(1,0)
J2, k 2 swap(2,1)
J3, no swap
J4, k 4..1 swap(k,k-1)
J5, k 5..3 swap(k,k-1)
15Quick sort
- Divide and conquer paradigm
- Divide, Conquer and Combine
- Divide The array Ap..r is paritioned into two
non-empty subarrays Ap..q and Aq1 ..r such
that each element of Ap..q is less or equal to
each element of Aq1..r.
16Quick Sort
- Conquer Each of the two subarrays Ap..q and
Aq1 ..r is sorted by recursive calls to
quicksort. - Combine Since the subarrays are sorted in place,
no work is needed.
17Partitioning
- Pivot is an element around which the
partitioning is performed. At first Ap is
selected as a pivot. - The algorithm grows two regions Ap..i and
Aj..r, such that every element of Ap..i is
less or equal to pivot and every element of Aj
..r is greater or equal to pivot.
18QuickSort
- void quick_sort(int left, int right)
-
- int i, last
- if (left gt right)
- return
- swap(left, (left right) / 2) / select the
pivot / - last left
- for (i left 1 i lt right i) /
partition / - if (bufferi lt bufferleft)
- swap(last, i)
- swap(left, last)
- quick_sort(left, last - 1) / sort left /
- quick_sort(last 1, right) / sort right /
Recursion terminating condition
Select the pivot
last stores the index of the last element that
is smaller than pivot.
19 5
2
4
6
1
3
Pivot
last 0
for (i left 1 i lt right i) if
(bufferi lt bufferleft) swap(last,
i) swap(left, last)
last 2
last 3
last
20left 0, right 2 swap(left,
(leftright) / 2)
3
2
1
last 0
Pivot
for (i left 1 i lt right i) if
(bufferi lt bufferleft) swap(last,
i) swap(left, last)
2
3
1
2
3
1
last 1
2
1
3
Last
1
2
3
21Complexity
- Worst case occurs when the partitioning produces
one region with n-1 elements and one with 1
element. - If the partitioning is maximally unbalanced at
every recursive step, the running is O(n2). - Whenever the split has constant proportionality
the running time is O(nlogn).