-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
### Instructions | ||
The goal of this activity is to explore the use of threads by creating a program for sorting integers that uses four goroutines to create four sub-arrays and then merge the arrays into a single array. | ||
|
||
### Detail Instruction | ||
Write a program to sort an array of integers. The program should partition the array into 4 parts, each of which is sorted by a different goroutine. Each partition should be of approximately equal size. Then the main goroutine should merge the 4 sorted subarrays into one large sorted array. | ||
|
||
The program should prompt the user to input a series of integers. Each goroutine which sorts ¼ of the array should print the subarray that it will sort. When sorting is complete, the main goroutine should print the entire sorted list. | ||
|
||
### Review criterialess | ||
Students will receive five points if the program sorts the integers and five additional points if there are four goroutines that each prints out a set of array elements that it is storing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
// https://stackoverflow.com/questions/39565055/read-a-set-of-integers-separated-by-space-in-golang | ||
fmt.Println("Please enter moret than 4 integers") | ||
var n int | ||
if m, err := fmt.Scan(&n); m != 1 { | ||
panic(err) | ||
} | ||
fmt.Println("Enter the integerss") | ||
sli := make([]int, n) | ||
ReadN(sli, 0, n) | ||
|
||
} | ||
|
||
// ReadN dfd | ||
func ReadN(sli []int, i, n int) { | ||
if n == 0 { | ||
return | ||
} | ||
if m, err := fmt.Scan(&sli[i]); m != 1 { | ||
panic(err) | ||
} | ||
// 1,2,3, 4- 3,2,1, 0 | ||
// e.g subsequent call when n=4 as initial value | ||
ReadN(sli, i+1, n-1) | ||
} |