Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

HO CHI MINH UNIVERSITY OF TECHNOLOGY

FALCUTY OF COMPUTER SCIENCE AND ENGINEERING

COMPUTER ARCHITECTURE
ASSIGNMENT
TOPIC:
QUICK SORT AND BINARY SEARCH

Instructor: Mr. Pham Quoc Cuong


Student: Phan Minh Toan
Studend ID: 1852798
Class: CC18KTM2

Ho Chi Minh City, May 2020


Table of contents
I. Problem. ....................................................................................... 3

II. Quicksort Algorithm. .................................................................. 3

1. Definition of Quicksort: ................................................................ 3

2. Algorithm. ................................................................................... 4

III. Binary Search Algorithm. ........................................................... 6

1. Definition of Binary Search. .......................................................... 6

2. Algorithm. ................................................................................... 6

IV. Solutions: ................................................................................... 7

1. Solution for problem 1: ................................................................. 7

2. Time measurement in submitted code. ............................................ 8

3. Solution for problem 2: ................................................................. 8

V. Conclusion .................................................................................... 9

VI. References. ............................................................................... 10

Computer Architecture – Assignment 2020 Page 2


I. Problem.
 Problem 1:

Given the following MIPS declaration in the data section of a MIPS program:

.data
nums .word <an integer number>
elems .word <array elements>

Where <an integer number> will store the number of elements in the array elems. elems is an integer
array whose size is equal to value <an integer number>. You are required to choose those values
when developing and testing your program.

1. Write a MIPS program that sort the the array elems in descending order using the quick sort
algorithm.
2. Calculate the execution time of your program if one instruction requires 1 ns for processing

 Problem 2:

.data
nums .word <an integer number>
elems .word <array elements>

Where <an integer number> will store the number of elements in the array elems. elems is an integer
array whose size is equal to value <an integer number>. You are required to choose those values
when developing and testing your program. Write a MIPS program that:

1. Sort the the array elems in ascending order at first.


2. Allow users to input an integer number. The program reports position(s) of the number if it
exists in the array. Binary search mus be used in this step.

II. Quicksort Algorithm.


1. Definition of Quicksort:

Computer Architecture – Assignment 2020 Page 3


Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as
a systematic method for placing the elements of a random access file or an array in order. Developed
by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used
algorithm for sorting. When implemented well, it can be about two or three times faster than its main
competitors, merge sort and heap-sort[1] .Quicksort is a comparison sort, meaning that it can sort
items of any type for which a "lessthan" relation (formally, a total order) is defined. Efficient
implementations of Quicksort are not a stable sort, meaning that the relative order of equal sort items
is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of
memory to perform the sorting. It is very similar to selection sort, except that it does not always
choose worst-case partition[1]. Mathematical analysis of quicksort shows that, on average, the
algorithm takes O(n.log(n)) comparisons to sort n items. In the worst case, it makes O(n2)
comparison [1].

2. Algorithm.

Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller
sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the
subarrays. The steps are:

 Pick an element from the array, called Pivot, which is the highest value in the value.
 Partitioning: reorder the array so that all elements with value less than the pivot come before
the pivot, while all elements with values greater than the pivot come after it (equal values can
go either way). After partitioning, the pivot is in its final position. This is called the partition
operation.
 Recursion is applied in the above steps to the sub-array of the elements with smaller values
and seperately to the sub-array of elements with greater values.
 Quicksort process can be divided into 2 parts:

- Partition:

Partition can be performed in many ways. However, they still follow the principle of below steps.

 Step 1: Choose the highest index value has pivot.


 Step 2: Take two variables to point left and right of the list excluding pivot.
 Step 3: Left points to the low index.
 Step 4: Right points to the high.

Computer Architecture – Assignment 2020 Page 4


 Step 5: While value at left is less than pivot move right.
 Step 6: While value at right is greater than pivot move left.
 Step 7: If both step 5 and step 6 does not match swap left and right.
 Step 8: If left greater or equal right, the point where they met is new pivot.

This is illustrated under an pseudo code:

Figure 1: Partition pseudo code 1

- Quicksort:
 Step 1: Make the right-most index value pivot.
 Step 2: Partition the array using pivot value.
 Step 3: Recursively Quicksort the left partition.
 Step 4: Recursively Quicksort the right partition.

Quicksort can be performed under a pseudo code:

Figure 2: Quick sort pseudo code 2

Computer Architecture – Assignment 2020 Page 5


III. Binary Search Algorithm.
1. Definition of Binary Search.

In computer science, binary search is a type of method used for finding a certain element in
a sorted list. It is an algorithm that is more efficient than linear search, which will go through
every element in the list of items to find a particular element. However, the given list of elements
must be sorted first before a binary search algorithm can be used[3] . The binary search algorithm
works by repeatedly splitting the sorted list into two and working on the part of the list that may
contain the element that you are looking for, until the final list contains only one element[3].

2. Algorithm.

Binary search compares the target value to the middle element of the array. If they are not
equal, the half in which the target cannot lie is eliminated and the search continues on the
remaining half, again taking the middle element to compare to the target value, and repeating this
until the target value is found. If the search ends with the remaining half being empty, the target is
not in the array[4] .

Binary Search can be performed under these steps:

 Step 1: Compare the input with the middle element.


 Step 2: If matches with middle element, return the mid index.
 Step 3: Input is greater recur for the right half.
 Step 4: Input is smaller recur for the left half.

Binary Search can be unstood through this pseudo code.

Figure 3: Binary search pseudo code

Computer Architecture – Assignment 2020 Page 6


IV. Solutions:
1. Solution for problem 1:

Write a MIPS program that sort the the array elems in descending order using the quick sort
algorithm.

My method for using quicksort to arrange in descending order can be described as follows:

The Quicksort always start with the Partition first before recursively call its to quicksort. For this
reason, I will start with the Partition first.

Partition in this descending order is to swap the the smaller the the end and the greater to the
front after comparing with the Pivot. By doing this, we must have an index points to the smaller and
an index for the greater, calling [i] the index for the smaller and [j] for the other. Now we have, Pivot
is the highest value in the current array; Arr[i] points to the first values in the array, so as Arr[j].
I now consider 2 cases that will happen in my method.

The first case can be described that the Arr[j] is greater than the Pivot value.

The second case is that Arr[j] is smaller than the Pivot value

To begin with the second case first, while j points to the value that is smaller than pivot, which
means nothing happen. The index i still points to same value. Meanwhile, j will point to the next
value to compare the the pivot. i will not point to any other value unless j points to a value that is
greater than Pivot.

The first case now can be illustrated as if the j index points the value that greater than the Pivot. We
now swap the the position of the current index i and j to a new one. From that, i now can point to the
next value in the array.

This happen until the index j points to the second highest value in the array. After that, we can swap
the value where i is currently pointing to with the pivot. We now can create 2 partition after this.

- Moving on to the Quicksort.

Quicksort is rather simple whether it only need to call the value where the Pivot is pointing to after
the Partition. And then call its recursively to quicksort the part before the pivot, and the part after the
pivot.

Computer Architecture – Assignment 2020 Page 7


2. Time measurement in submitted code.

Instruction counts in submitted code with 18 elements are:

R-types 1054 33%


I-types 1834 59%
J-types 214 6%
Total instructions: 3102
Table 1: Instructions Data

Execution time can be calculate as follow:

From that, execution time is: 3102 nano second.

3. Solution for problem 2:

Sort the array in the ascending order.

To sort the array in ascending order, I use a sequence of reordering the smallest element in the array
to the very top. This can be described by:

Generating a loop for the to replace each index from lowest to highest. However, if the value we
need to replace is the lowest, there is no need to change it, the only thing we need to do is to move on
to the next element. This loop will start from the very first element to the very last element. From the
next element, starting from the element we need to change, we will find the smaller value to that
element to get the smallest. From that smallest, swapping the 2 elements is operated, the value we
need to change and the smallest, occur.

Allow user to enter an number and find all of its position using Binary Search in the sorted array.

The basic idea of binary search is described as follow:

The princible of the code still follow the the basic princible of the Binary Search method, which are:

 Step 1: Compare the input with the middle element.


 Step 2: If matches with middle element, return the mid index.
 Step 3: Input is greater recur for the right half.
 Step 4: Input is smaller recur for the left half.

Computer Architecture – Assignment 2020 Page 8


However, in this specific request, we need to find all the positions of the element if it existed. For
this reason, the aforementioned princible is still applied with extensive modification. This
modification can be simply understood as we need to find the highest position of that element and
also the smallest. All the positions lie between this range are the positions of the element.

V. Conclusion

For Quick Sort, it is able to deal well with a huge list of items and because it sorts in place, no
additional storage is required as well and the slight disadvantage is that its worst-case performance is
similar to average performances of the bubble, insertion or selections sorts,...

For Binary Search, it is much faster than other method like Linear Search, simple algorithm and the
disadvantage is more complicated than Linear Search, and is overkill for very small numbers of
elements. It recursively sorted.

But in conclusion, both Quick Sort and Binary Search are useful to solve some complex problems as
long as the user know to combine or use it efficiently.

Computer Architecture – Assignment 2020 Page 9


VI. References.

[1] Quicksort, WIKIPEDIA,

https://en.wikipedia.org/wiki/Quicksort

[2] Data Structure and Algorithms - Quick Sort, tutorialpoint,

https://www.tutorialspoint.com/data_structures_algorithms/quick_sort_algorithm.htm

[3] Binary search, Simple English WIKIPEDIA,

https://simple.wikipedia.org/wiki/Binarysearch

[4] Binary search algorithm, WIKIPEDIA,

https://en.wikipedia.org/wiki/Binarysearchalgorithm

Computer Architecture – Assignment 2020 Page 10

You might also like