Skip to content

Commit

Permalink
More details for array problems (#168)
Browse files Browse the repository at this point in the history
* Explain Bubble Sort and Insertion Sort
* Add more examples to Array rehearsal problems
* More clear wording in README.md
  • Loading branch information
spring1843 authored Jan 20, 2025
1 parent 18820b8 commit 3293b70
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 13 deletions.
4 changes: 2 additions & 2 deletions array/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Array

Arrays are a fundamental and essential data structure in computer science. They consist of a fixed-size contiguous memory block and offer O(1) read and write time complexity. As a fundamental element of programming languages, arrays come built-in with their core.
Arrays are a fundamental and essential data structure in computer science. They consist of a fixed-size contiguous memory block and offer O(1) read and write time complexity. In Go Like most other programming languages arrays are a built-in data type.

To provide a real-world analogy, consider an array of athletes preparing for a sprinting match. Each athlete occupies a specific position within the array, typically denoted as 1, 2,…, n. While it is technically possible for each athlete to be in a different position, the positions generally carry some form of significance, such as alphabetical order or seniority within the sport.

Expand All @@ -22,7 +22,7 @@ func main() {

Although arrays are fundamental data structures in Go, their constant size can make them inflexible and difficult to use in situations where a variable size is required. To address this issue, Go provides [slices](https://blog.golang.org/slices-intro), an abstraction of arrays that offer more convenient access to sequential data typically stored in arrays. When a slice is passed to a function, only the slice header is passed, but it still references the same underlying array data, hence it is possible for the callee to modify the values of the slice and send them back to the caller.

Slices enable adding values using the `append` function, allowing dynamic resizing. Additionally, selectors of the format [low:high] can be used to select or manipulate data in the slice. By utilizing slices instead of arrays, Go programmers gain a more flexible and powerful tool to manage their data structures.
Slices enable adding values using the `append` function, allowing dynamic resizing. Additionally, selectors of the format [low:high] can be used to select or manipulate data in the slice. By utilizing slices instead of arrays, Go programmers benefit from a more flexible and powerful tool to manage their data structures.

```Go
package main
Expand Down
4 changes: 2 additions & 2 deletions array/add_two_numbers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ TestAddTwoNumbers tests solution(s) with the following signature and problem des
AddTwoNumbers(num1, num2 []int) []int
Given two numbers as an array like [2,9] and [9,9,9] return the sum of the numbers they
represent like [1,0,2,8], because 29+999=1028.
Given two positive integers represented as a slice like {2,9} and {9,9,9} return their sum like {1,0,2,8}.
because 29+999=1028.
*/
func TestAddTwoNumbers(t *testing.T) {
tests := []struct {
Expand Down
22 changes: 21 additions & 1 deletion array/bubble_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ TestBubbleSort tests solution(s) with the following signature and problem descri
BubbleSort(input []int)
Given an array of unsorted integers, sort the array using the Bubble Sort algorithm.
Sort a slice of unsorted integers using the Bubble Sort algorithm.
The algorithm should be in-place, meaning it should not create a new array and it should
work by swapping elements in the array until it is sorted.
To use Bubble Sort to increasingly sort a slice:
1- Go through each item of the list.
2- If item at n[i] is larger than item at n[i+1], then swap the two elements and repeat the same process for every remaining element(s).
3- If a swap has happened go back to 1, otherwise the list is sorted.
The name Bubble Sort comes from the observation that smaller values bubble up to the top of the list and larger values sink lower until
the list is sorted.
To sort {2,3,1} using Bubble Sort:
1- Compare 2 and 3. Do not swap 2 and 3 because 2 <3.
2- Swap 3 and 1 because 1 < 3, the list becomes {2,1,3}.
3- Swap 1 and 2 because 1 < 2, the list becomes {1,2,3}.
4- Do not swap 2 and 3 because 2 < 3.
5- Do not swap 1 and 2 because 1 < 2.
6- Do not swap 2 and 3 because 2 < 3.
7- The list is sorted, return {1,2,3}.
*/
func TestBubbleSort(t *testing.T) {
tests := []struct {
Expand Down
2 changes: 2 additions & 0 deletions array/equal_sum_subarrays_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ TestEqualSumSubArrays tests solution(s) with the following signature and problem
Given an list of integers A, return two sub-arrays with equal sums without changing the
order of the elements in the list.
For example if given {1,7,3,5} return {1,7} and {3,5} because 1+7 = 3+5 = 8.
*/
func TestEqualSumSubArrays(t *testing.T) {
tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions array/find_duplicate_in_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ TestFindDuplicate tests solution(s) with the following signature and problem des
FindDuplicate(list []int) int
Given a list of n integers (3, 2, 1, 4, 5, 4,...,n) where each number is positive and smaller
than n find the duplicate integer.
Given an unsorted slice of n positive integers like {3,2,1,4,5,4,...,n} where each number is smaller
than n and there is at most one duplicate, return the duplicate value like 4.
*/
func TestFindDuplicate(t *testing.T) {
tests := []struct {
Expand Down
21 changes: 18 additions & 3 deletions array/insertion_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,24 @@ TestInsertionSort tests solution(s) with the following signature and problem des
InsertionSort(input []int)
Given an array of unsorted integers, sort the array using the Insertion Sort algorithm.
The algorithm should be in-place, meaning it should not create a new array. The algorithm
works by dividing the input array into sorted and unsorted sections, and moving items from
the unsorted section into the sorted section, one item at a time.
The algorithm should be in-place, meaning it should not create a new array.
To increasingly sort a slice using Insertion Sort:
1- Create a sorted list and an unsorted list. Put the first element of the list in the sorted and the rest of the elements in the unsorted list.
2- Compare the first element of the unsorted list with each element of the sorted list starting from the end and swapping as needed.
3- If there are remaining elements in the unsorted list keep doing 2.
The name Insertion Sort comes from the observation that each element is picked and inserted at the right place.
To save memory space in an in-place implementation of Insertion Sort we use the left side and right side of the input list
and an imaginary divisor as a virtual way of having two separate lists.
To sort {2,3,1} using Insertion Sort:
1- Put 2 in the sorted list and the remaining elements {3,1} in the unsorted list.
2- Do not swap 3 and 2 since 2<3, add 3 to the sorted list so the sorted list is {2,3} and the unsorted list is {1}.
3- Swap 3 and 1 since 1<3 so the sorted list is {2,1,3} and unsorted list is empty. Continue by comparing 1 and 2 and swapping since 1<2 so the list becomes {1,2,3}.
4- Since there are no elements left in the unsorted list, the list is sorted so return {1,2,3}.
*/
func TestInsertionSort(t *testing.T) {
tests := []struct {
Expand Down
6 changes: 6 additions & 0 deletions array/product_of_all_other_elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ TestProductOfAllOtherElements tests solution(s) with the following signature and
Given an array of integers A, construct a new array B such that B[i] = product of all items
in A except A[i] without using division in O(n) time.
For example given {1,2,3,4} it should return {24,12,8,6} because:
* 24=2*3*4.
* 12=1*3*4.
* 8=1*2*4.
* 6=1*2*3.
*/
func TestProductOfAllOtherElements(t *testing.T) {
tests := []struct {
Expand Down
9 changes: 7 additions & 2 deletions array/reverse_inplace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ TestReverseInPlace tests solution(s) with the following signature and problem de
ReverseInPlace(list []int, start, end int)
Given an array of integers, a start index, and an end index, reverse the integers in the
array in-place without using any extra memory.
Given a slice of integers, a start index, and an end index, reverse the integers in the in-place
without using any extra memory.
For example given {1,2,3,4,5,6} and start of 0 and end of 4, it should return {5,4,3,2,1,6} because:
Reverse of items from index 0 to 4 is {5,4,3,2,1} and the remaining item {6} remain unchanged, so
the the resulting slice is {5,4,3,2,1,6}.
*/
func TestReverseInPlace(t *testing.T) {
tests := []struct {
Expand Down
5 changes: 5 additions & 0 deletions array/rotate_k_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ TestRotateKSteps tests solution(s) with the following signature and problem desc
RotateKSteps(list []int, k int)
Given an list of integers and a number k, rotate the array k times.
For example if given {1,2,3} and 3, it should return {1,2,3} because.
Slice after 1 rotation: {3,1,2}.
Slice after 2 rotations: {2,3,1}.
Slice after 3 rotations: {1,2,3}.
*/
func TestRotateKSteps(t *testing.T) {
tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion array/zero_sum_triplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TestZeroSumTriplets tests solution(s) with the following signature and problem d
ZeroSumTriplets(list []int) [][]int
Given an array of numbers like {1, 2, -4, 6, 3} returns unique triplets from the numbers
with sum that equals zero like {-4, 1, 3}.
with sum that equals zero like {-4, 1, 3} because -4+1+3=0.
*/
func TestZeroSumTriplets(t *testing.T) {
tests := []struct {
Expand Down

0 comments on commit 3293b70

Please sign in to comment.