Przeglądaj źródła

Added java solution to week1

Trinh Tong 6 lat temu
rodzic
commit
c860fa5f80
1 zmienionych plików z 18 dodań i 2 usunięć
  1. 18
    2
      Week 1.md

+ 18
- 2
Week 1.md Wyświetl plik

@@ -35,7 +35,7 @@ Output:
35 35
 `{ -55, -72, -1, 2, 5, 8, 53 }`
36 36
 
37 37
 
38
-*/ Trinh's Solution
38
+Trinh's Solution
39 39
 Nested for loop to bubble sort?
40 40
 We can start at index 0, and start comparing,
41 41
 with each pass of the array, it will slowly move the elements
@@ -48,6 +48,22 @@ but rather just if it's positive or negative?
48 48
 
49 49
 
50 50
 
51
+public static void sortLoop(int[] inputArray) {
52
+int tempVal = inputArray[0];
51 53
 
54
+for (int i = 0; i < inputArray.length - 1; i++) {
55
+# the first for loop gets the first number and actively compares it to the next through the next for loop
56
+# using the tempVal variable we don't lose the value we replace and can then swap it to the right position.
52 57
 
53
-/*
58
+/
59
+    for (int j = 0; j < inputArray.length - i; j++) {
60
+        if (inputArray[j] > inputArray[i]) {
61
+            tempVal = inputArray[j];
62
+            inputArray[j] = inputArray[j+1];
63
+            tempVal = inputArray[j+1];
64
+        }
65
+    }
66
+}
67
+
68
+  
69
+}