Bläddra i källkod

Added java solution to week1

Trinh Tong 6 år sedan
förälder
incheckning
c860fa5f80
1 ändrade filer med 18 tillägg och 2 borttagningar
  1. 18
    2
      Week 1.md

+ 18
- 2
Week 1.md Visa fil

35
 `{ -55, -72, -1, 2, 5, 8, 53 }`
35
 `{ -55, -72, -1, 2, 5, 8, 53 }`
36
 
36
 
37
 
37
 
38
-*/ Trinh's Solution
38
+Trinh's Solution
39
 Nested for loop to bubble sort?
39
 Nested for loop to bubble sort?
40
 We can start at index 0, and start comparing,
40
 We can start at index 0, and start comparing,
41
 with each pass of the array, it will slowly move the elements
41
 with each pass of the array, it will slowly move the elements
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
+}