Docsity

Sorting Algorithm Homework Assignment, Assignments of Computer Science

Toggle sidebar
bg1
Homework 2, due Jun 29th, before class
Since this homework is more complex than the previous one, it will be worth 10 points!
Sorting
Sorting is a very important problem in Computer Science. It can be defined as:
Input: List of elements (ei) that can be compared, e.g. ints
Output: List of elements (fj) such that:
• ∀ei∃j:fj=ei:
All elements in (ei) must also show up in (fj).
• ∀j, k :j < k →fj≤fk:
Elements are sorted in ascending order.
We will use arrays of ints to model the list of elements:
int[] e;
The elements can be easily compared by using the well-known operators:
e[i]<e[j]
Your task is to develop an algorithm (either textual or Java code), that takes as input an
array of ints and returns as output an array of ints that contains the same elements, but in
ascending order. You may assume that the elements are pairwise unequal, that is, an element
does not appear twice in the input.
Examples:
1. Input: {1,2,3,4,5}
Output: {1,2,3,4,5}
2. Input: {5,4,3,2,1}
Output: {1,2,3,4,5}
3. Input: {1,4,2,3}
Output: {1,2,3,4}
4. Input: {5,9,1}
Output: {1,5,9}
As seen in the example, your algorithm has to work on arbitrary-length arrays (though you
should start on a short, constant array length, e.g., 4).
Before class on the due date, hand in your algorithm (either in text or as source code; working
source code may give extra credit) as well as some text explaining your algorithm (e.g., how
and why it works). All in all, one page should be fine. If you do use resources and not only
your brain, list them.
Furthermore, you need to be prepared to present your algorithm to the class in about 5-15
minutes. I have scheduled two to four presentations for class. Voluntary presentation will be
awarded with extra credit, while bad performance will reflect negatively on your homework
score.

Partial preview of the text

Download Sorting Algorithm Homework Assignment and more Assignments Computer Science in PDF only on Docsity!

Homework 2, due Jun 29th, before class

Since this homework is more complex than the previous one, it will be worth 10 points!

Sorting

Sorting is a very important problem in Computer Science. It can be defined as:

Input: List of elements (ei) that can be compared, e.g. ints Output: List of elements (fj ) such that:

  • ∀ei∃j : fj = ei: All elements in (ei) must also show up in (fj ).
  • ∀j, k : j < k → fj ≤ fk: Elements are sorted in ascending order.

We will use arrays of ints to model the list of elements: int[] e;

The elements can be easily compared by using the well-known operators: e[i]<e[j]

Your task is to develop an algorithm (either textual or Java code), that takes as input an array of ints and returns as output an array of ints that contains the same elements, but in ascending order. You may assume that the elements are pairwise unequal, that is, an element does not appear twice in the input.

Examples:

  1. Input: {1,2,3,4,5} Output: {1,2,3,4,5}
  2. Input: {5,4,3,2,1} Output: {1,2,3,4,5}
  3. Input: {1,4,2,3} Output: {1,2,3,4}
  4. Input: {5,9,1} Output: {1,5,9}

As seen in the example, your algorithm has to work on arbitrary-length arrays (though you should start on a short, constant array length, e.g., 4).

Before class on the due date, hand in your algorithm (either in text or as source code; working source code may give extra credit) as well as some text explaining your algorithm (e.g., how and why it works). All in all, one page should be fine. If you do use resources and not only your brain, list them.

Furthermore, you need to be prepared to present your algorithm to the class in about 5- minutes. I have scheduled two to four presentations for class. Voluntary presentation will be awarded with extra credit, while bad performance will reflect negatively on your homework score.