Skip to content

Commit

Permalink
A1
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierrySans committed Sep 9, 2017
1 parent 2dd2839 commit 91a8b7b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions _data/work.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- week: Version Control with Git
type: individual
handout: 01/

- week: Team Setup
type: team
Expand Down
45 changes: 45 additions & 0 deletions work/01/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: default
permalink: /work/01/
---

# Individual Assignment 1

## Github repositories

It is recommended to get a local copy of the following three Github repositories:

- **the CSCC01 course repository** (public repo - read only)

In this repository you will find the course materials and starter code for the different deliverables. To clone this repository:

```
$ git clone https://github.com/ThierrySans/CSCC01
```
- **your personal repository** (accessible by you and the course staff)
In this repository, you will push the solution for your individual assignments. To clone this repository:
```
$ git clone https://github.com/CSCC01F17/__your_email_preffix__
```
if your email is [email protected], the __your_email_preffix__ should be john.doe
- **your team repository** (accessible by you, your team members and the course staff)
In this repository, you will push your project deliverables. To clone this repository:
```
$ git clone https://github.com/CSCC01F17/__team__id__
```
## Instructions
1.1 If you have not done so yet, clone the course repository and your personal repository. In your personal repository, copy the starter code (`/work/01/src/MergeSort.java`) from the course repository into yours (`/01/src/MergeSort.java`).
1.2 Test this file and fix the bugs
1.3 Commit the bug fix, with an appropriate commit message. Push.
77 changes: 77 additions & 0 deletions work/01/src/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package mergesort;

import java.util.List;
import java.util.ArrayList;
import java.util.Random;

public class MergeSort {

/**
* @param args
*/
public static void main(String[] args) {
List<Integer> myList = new ArrayList<Integer>();
Random rd = new Random();

for (int i = 1; i < 10; i++) {
myList.add(rd.nextInt(10) + 1);
}

System.out.println("UnSorted: " + myList);
msort(myList);
System.out.println("Sorted: " + msort(myList));
}

/**
* Merge Sort! Non-decreasing order.
* @param unsorted
* @return A List that contains all elements from unsorted,
* sorted in non-decreasing order.
*/
public static List<Integer> msort(List<Integer> unsorted) {
if (unsorted.size() <= 1) {
return unsorted;
}

List<Integer> left = new ArrayList<Integer>();
List<Integer> right = new ArrayList<Integer>();

for (int i = 0; i < (unsorted.size() / 2); i++) {
left.add(unsorted.get(i));
}
for (int i = unsorted.size() / 2; i < unsorted.size(); i++) {
right.add(unsorted.get(i));
}

msort(left);
msort(right);

return merge(left,right);
}

/**
* Merge for mergesort.
* @param left The left sub-list. Sorted in non-decreasing order.
* @param right The right sub-list. Sorted in non-decreasing order.
* @return A List that contains all elements from left and right,
* sorted in non-decreasing order.
*/
public static List<Integer> merge(List<Integer> left, List<Integer> right) {

List<Integer> fin = new ArrayList<Integer>();
// pointers
int lp = 0, rp = 0, fp = 0;

while (lp < left.size() && rp < right.size()) {
if (left.get(lp) < right.get(rp)) {
fin.add(left.get(lp));
lp++;
} else {
fin.add(right.get(rp));
rp++;
}
fp++;
}
return fin;
}
}

0 comments on commit 91a8b7b

Please sign in to comment.