├── .gitignore ├── Basic algo ├── Ackerman.java ├── Binomial.java ├── Factorial.java ├── Fibonacci.java ├── GCD.java ├── Hanoi.java └── Permutation.java ├── BinaryAverageRecursion.java ├── BinaryRecursiveArray.java ├── Combination.java ├── DataStructure實作 ├── BinaryTree.java ├── ListDemo.c ├── ListDemo.java ├── Stack.c └── StackuseLinklist.c ├── DecimalToOther.java ├── LCS_DP.java ├── LCS_Recursive.java ├── Matrix相關 ├── MatrixMultiply.c └── MatrixTranspose.java ├── NCCU-DCT ├── Caesar.js ├── NCCU106.java └── canvas.html ├── NCCU └── 100 │ └── 計概 │ └── Two.java ├── NCU ├── 100 │ └── 計概 │ │ └── Test.java ├── 102 │ └── 計概 │ │ ├── Question1Test.java │ │ ├── Question2.java │ │ └── Question2_Solve.java ├── 104 │ └── 計概 │ │ └── Night.java └── 106 │ └── 計概 │ └── Eight.java ├── NSYSU ├── 106 │ └── TwoStringCombine.c └── NSYSU.java ├── OO_practice └── Object_oriented_practice.java ├── README.md ├── RecurAverage.java ├── Search ├── BinarySearch.c └── Binarysearch.java ├── Sort ├── BubbleSort.java ├── CountingSort.java ├── InsertionSort.java ├── MergeSort.java ├── QuickPartitionAlgo.java ├── QuickSort.java └── SelectionSort.java ├── StringRecursiveReverse.c └── StringReverse.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | -------------------------------------------------------------------------------- /Basic algo/Ackerman.java: -------------------------------------------------------------------------------- 1 | public class Ackerman { 2 | public static void main(String[] argv) { 3 | int m = 2, n = 2; 4 | System.out.println("Ackerman(" + m + "," + n + ") = " + Ackerm(m ,n)); 5 | } 6 | public static int Ackerm(int m, int n) { 7 | if(m == 0) 8 | return n + 1; 9 | else if(n == 0) 10 | return Ackerm(m - 1, 1); 11 | else { 12 | return Ackerm(m - 1, Ackerm(m, n - 1)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Basic algo/Binomial.java: -------------------------------------------------------------------------------- 1 | public class Binomial { 2 | public static void main(String[] argv) { 3 | int m = 4, n = 2; 4 | //recursive版 5 | System.out.println("C" + m + "取" + n + " : " + C_RE(m, n)); 6 | //Imperative版 7 | System.out.println("C" + m + "取" + n + " : " + C(m, n)); 8 | } 9 | public static int C_RE(int m, int n) { 10 | int n_fac = 1, m_fac = 1, m_n_fac = 1; 11 | for(int i = 1; i <= m; i++) { 12 | m_fac *= i; 13 | } 14 | for(int i = 1; i <= n; i++) { 15 | n_fac *= i; 16 | } 17 | for(int i = 1; i <= m - n; i++) { 18 | m_n_fac *= i; 19 | } 20 | return m_fac / (m_n_fac * n_fac); 21 | } 22 | public static int C(int m, int n) { 23 | if(m == n || n == 0) 24 | return 1; 25 | else 26 | return C(m - 1, n - 1) + C(m - 1, n); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Basic algo/Factorial.java: -------------------------------------------------------------------------------- 1 | public class Factorial { 2 | public static void main(String[] argv) { 3 | int n = 5; 4 | //Imperative版 5 | System.out.println(n + "! = " + factorial(n)); 6 | //Recursive版 7 | System.out.println(n + "! = " + factorial_Re(n)); 8 | } 9 | public static int factorial(int n) { 10 | int ans = 1; 11 | for(int i = 1; i <= n; i++) { 12 | ans *= i; 13 | } 14 | return ans; 15 | } 16 | public static int factorial_Re(int n) { 17 | if(n <= 0) 18 | return 1; 19 | else { 20 | return n * factorial_Re(n - 1); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Basic algo/Fibonacci.java: -------------------------------------------------------------------------------- 1 | public class Fibonacci { 2 | public static void main(String[] argv) { 3 | int n = 6; 4 | //Dynamic Programming版本 5 | System.out.println("費氏數列 F(" + n + ") = " + Fibo_DP(n)); 6 | //Recursive版本 7 | System.out.println("費氏數列 F(" + n + ") = " + Fibo_Re(n)); 8 | //Imperative版本 9 | System.out.println("費氏數列 F(" + n + ") = " + Fibo_Im(n)); 10 | } 11 | public static int Fibo_Im(int n) { 12 | int a = 0, b = 1, c = 0; 13 | for(int i = 2; i <= n; i++) { 14 | c = a + b; 15 | a = b; 16 | b = c; 17 | } 18 | return c; 19 | } 20 | public static int Fibo_Re(int n) { 21 | if(n == 0) 22 | return 0; 23 | else if(n == 1) 24 | return 1; 25 | else { 26 | return Fibo_Re(n - 1) + Fibo_Re(n - 2); 27 | } 28 | } 29 | public static int Fibo_DP(int n) { 30 | int[] F = new int[n + 1]; 31 | F[0] = 0; 32 | F[1] = 1; 33 | if(n == 0) 34 | return 0; 35 | else if (n == 1) 36 | return 1; 37 | else { 38 | for(int i = 2; i <= n; i++) { 39 | F[i] += F[i - 1] + F[i - 2]; 40 | } 41 | } 42 | return F[n]; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Basic algo/GCD.java: -------------------------------------------------------------------------------- 1 | 2 | //找最大公因數 3 | public class GCD { 4 | public static void main(String[] argv) { 5 | int A = 95, B = 15; 6 | //Recursive 7 | System.out.println(A + " 和 " + B + " 之最大公因數為: " + GCD_R(A, B)); 8 | //Imperative 9 | System.out.println(A + " 和 " + B + " 之最大公因數為: " + GCD_I(A, B)); 10 | } 11 | //Recursive 12 | public static int GCD_R(int A, int B) { 13 | if(A % B == 0) { 14 | return B; 15 | } else { 16 | return GCD_R(B, A % B); 17 | } 18 | } 19 | //Imperative 20 | public static int GCD_I(int A, int B) { 21 | while(A != 0 && B != 0) { 22 | if(A > B) { 23 | A %= B; 24 | } else { 25 | B %= A; 26 | } 27 | } 28 | if(A == 0) { 29 | return B; 30 | } 31 | else { 32 | return A; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Basic algo/Hanoi.java: -------------------------------------------------------------------------------- 1 | public class Hanoi { 2 | public static void hanoi(int n, String from, String temp, String to) { 3 | if (n == 1) { 4 | System.out.println("move disc " + n + " from " + from + " to " + to); 5 | } else { 6 | hanoi(n - 1, from, to, temp); 7 | System.out.println("Move disc " + n + " from " + from + " to " + to); 8 | hanoi(n - 1, temp, from, to); 9 | } 10 | } 11 | public static void main(String[] args) { 12 | hanoi(3, "A", "B", "C"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Basic algo/Permutation.java: -------------------------------------------------------------------------------- 1 | public class Permutation { 2 | public static void main(String[] argv) { 3 | int[] array = {1,2,3}; 4 | perm(array, 0, array.length); 5 | } 6 | public static void perm(int[] array, int i, int n) { 7 | if(i == n) { 8 | for(int index:array) { 9 | System.out.print(index + " "); 10 | } 11 | System.out.println(); 12 | } 13 | for(int j = i; j < n; j++) { 14 | //Swap array[i] & array[j] 15 | int temp = array[i]; 16 | array[i] = array[j]; 17 | array[j] = temp; 18 | //perm() 19 | perm(array, i+1, n); 20 | //Swap array[i] & array[j] 21 | temp = array[i]; 22 | array[i] = array[j]; 23 | array[j] = temp; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BinaryAverageRecursion.java: -------------------------------------------------------------------------------- 1 | public class BinaryAverageRecursion { 2 | public static void main(String[] argv) { 3 | double[] A = { 1.0, 2.0, 3.0, 4.0, 5.0 }; 4 | System.out.println(BinaryAvg(A, 0, 5)); 5 | 6 | //NCCU 100年 7 | int[] Array = { 1, 2, 3, 4, 5 }; 8 | System.out.println(BinarySum(Array, 0, 5)); 9 | 10 | //NCCU 102年 11 | int[] Arr = { 1, 2, 3, 4, 5 }; 12 | System.out.println(BinaryMul(Arr, 0, 5)); 13 | } 14 | public static double BinaryAvg(double[] A, int start, int n) { 15 | if(n == 1) { 16 | return A[start]; 17 | } 18 | else if (n == 2) { 19 | return (A[start] + A[start + 1]) / 2; 20 | } 21 | else { 22 | double left, right; 23 | if( n % 2 == 0) { 24 | //陣列是偶數個的處理 25 | left = BinaryAvg(A, start, n/2); 26 | right = BinaryAvg(A, start + n/2, n/2); 27 | return (left * (n/2) + right * (n/2)) / n; 28 | } 29 | else { 30 | //陣列是奇數個的處理 31 | left = BinaryAvg(A, start, n/2); 32 | right = BinaryAvg(A, start + n/2 , n/2 + 1); 33 | return (left * (n/2) + right * (n/2 + 1)) / n; 34 | } 35 | 36 | } 37 | } 38 | public static int BinarySum(int[] Array, int i, int n) { 39 | if(n == 1) { 40 | return Array[i]; 41 | } 42 | else if (n == 2) { 43 | return Array[i] + Array[i + 1]; 44 | } 45 | else { 46 | int left, right; 47 | if( n % 2 == 0) { 48 | //陣列是偶數個的處理 49 | left = BinarySum(Array, i, n/2); 50 | right = BinarySum(Array, i + n/2, n/2); 51 | return left + right; 52 | } 53 | else { 54 | //陣列是奇數個的處理 55 | left = BinarySum(Array, i, n/2); 56 | right = BinarySum(Array, i + n/2 , n/2 + 1); 57 | return left + right; 58 | } 59 | } 60 | } 61 | public static int BinaryMul(int[] Array, int i, int n) { 62 | if(n == 1) { 63 | return Array[i]; 64 | } 65 | else if (n == 2) { 66 | return Array[i] * Array[i + 1]; 67 | } 68 | else { 69 | int left, right; 70 | if( n % 2 == 0) { 71 | //陣列是偶數個的處理 72 | left = BinaryMul(Array, i, n/2); 73 | right = BinaryMul(Array, i + n/2, n/2); 74 | return left * right; 75 | } 76 | else { 77 | //陣列是奇數個的處理 78 | left = BinaryMul(Array, i, n/2); 79 | right = BinaryMul(Array, i + n/2 , n/2 + 1); 80 | return left * right; 81 | } 82 | 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /BinaryRecursiveArray.java: -------------------------------------------------------------------------------- 1 | public class BinaryRecursiveArray { 2 | public static int binaryRecursiveSum(int[] data, int low, int high) 3 | { 4 | if( low > high ) 5 | return 0; 6 | else if( low == high ) 7 | return data[low]; 8 | else 9 | { 10 | int mid = (low + high) / 2; 11 | return binaryRecursiveSum(data, low, mid) + binaryRecursiveSum(data, mid+1, high); 12 | } 13 | } 14 | public static int binaryRecursiveMul(int[] data, int low , int high) 15 | { 16 | if( low > high ) 17 | return 1; 18 | else if (low == high) 19 | return data[low]; 20 | else { 21 | int mid = (low + high) / 2; 22 | return binaryRecursiveMul(data, low, mid) * binaryRecursiveMul(data, mid+1, high); 23 | } 24 | } 25 | public static void main(String[] args) { 26 | 27 | int[] data = {1, 2, 3, 4, 5}; 28 | 29 | int sumResult = binaryRecursiveSum(data, 0, data.length-1); 30 | int mulResult = binaryRecursiveMul(data, 0, data.length-1); 31 | System.out.println("Array 總和:" + sumResult); 32 | System.out.println("Array 總積:" + mulResult); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Combination.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Combination { 3 | //中央102考題11 4 | public static void main(String argv[]) { 5 | int sum = 0; 6 | Scanner input = new Scanner(System.in); 7 | int n = input.nextInt(); 8 | int m = input.nextInt(); 9 | sum = C(n, m); 10 | System.out.println("The result is " + sum); 11 | } 12 | public static int C(int n, int m) { 13 | if(m == 0 || n == m) 14 | return 1; 15 | else 16 | return C(n - 1, m) + C (n - 1, m - 1); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DataStructure實作/BinaryTree.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | /* Class BTNode */ 5 | class BTNode 6 | { 7 | BTNode left, right; 8 | String data; 9 | 10 | /* Constructor */ 11 | public BTNode() 12 | { 13 | left = null; 14 | right = null; 15 | data = ""; 16 | } 17 | /* Constructor */ 18 | public BTNode(String n) 19 | { 20 | left = null; 21 | right = null; 22 | data = n; 23 | } 24 | /* Function to set left node */ 25 | public void setLeft(BTNode n) 26 | { 27 | left = n; 28 | } 29 | /* Function to set right node */ 30 | public void setRight(BTNode n) 31 | { 32 | right = n; 33 | } 34 | /* Function to get left node */ 35 | public BTNode getLeft() 36 | { 37 | return left; 38 | } 39 | /* Function to get right node */ 40 | public BTNode getRight() 41 | { 42 | return right; 43 | } 44 | /* Function to set data to node */ 45 | public void setData(String d) 46 | { 47 | data = d; 48 | } 49 | /* Function to get data from node */ 50 | public String getData() 51 | { 52 | return data; 53 | } 54 | } 55 | 56 | /* Class BT */ 57 | class BT 58 | { 59 | private BTNode root; 60 | 61 | /* Constructor */ 62 | public BT() 63 | { 64 | root = null; 65 | } 66 | /* Function to check if tree is empty */ 67 | public boolean isEmpty() 68 | { 69 | return root == null; 70 | } 71 | /* Functions to insert data */ 72 | public void insert(String data) 73 | { 74 | root = insert(root, data); 75 | } 76 | /* Function to insert data recursively */ 77 | private BTNode insert(BTNode node, String data) 78 | { 79 | if (node == null) 80 | node = new BTNode(data); 81 | else 82 | { 83 | if (node.getRight() == null) 84 | node.right = insert(node.right, data); 85 | else 86 | node.left = insert(node.left, data); 87 | } 88 | return node; 89 | } 90 | /* Function to count number of nodes */ 91 | public int countNodes() 92 | { 93 | return countNodes(root); 94 | } 95 | /* Function to count number of nodes recursively */ 96 | private int countNodes(BTNode r) 97 | { 98 | if (r == null) 99 | return 0; 100 | else 101 | { 102 | int l = 1; 103 | l += countNodes(r.getLeft()); 104 | l += countNodes(r.getRight()); 105 | return l; 106 | } 107 | } 108 | /* Function to search for an element */ 109 | public boolean search(String val) 110 | { 111 | return search(root, val); 112 | } 113 | /* Function to search for an element recursively */ 114 | private boolean search(BTNode r, String val) 115 | { 116 | if (val.equals(r.getData())) 117 | return true; 118 | if (r.getLeft() != null) 119 | if (search(r.getLeft(), val)) 120 | return true; 121 | if (r.getRight() != null) 122 | if (search(r.getRight(), val)) 123 | return true; 124 | return false; 125 | } 126 | /* Function for inorder traversal */ 127 | public void inorder() 128 | { 129 | inorder(root); 130 | } 131 | private void inorder(BTNode r) 132 | { 133 | if (r != null) 134 | { 135 | if(r.getLeft() != null) { 136 | System.out.print(" ( "); 137 | } 138 | printExpression(r.getLeft()); 139 | System.out.print(r.getData() + " "); 140 | printExpression(r.getRight()); 141 | if(r.getRight() != null) { 142 | System.out.print(" ) "); 143 | } 144 | } 145 | } 146 | private void printExpression() 147 | { 148 | printExpression(root); 149 | } 150 | private void printExpression(BTNode r) 151 | { 152 | if (r != null) 153 | { 154 | printExpression(r.getLeft()); 155 | System.out.print(r.getData() + " "); 156 | printExpression(r.getRight()); 157 | } 158 | } 159 | /* Function for preorder traversal */ 160 | public void preorder() 161 | { 162 | preorder(root); 163 | } 164 | private void preorder(BTNode r) 165 | { 166 | if (r != null) 167 | { 168 | System.out.print(r.getData() +" "); 169 | preorder(r.getLeft()); 170 | preorder(r.getRight()); 171 | } 172 | } 173 | /* Function for postorder traversal */ 174 | public void postorder() 175 | { 176 | postorder(root); 177 | } 178 | private void postorder(BTNode r) 179 | { 180 | if (r != null) 181 | { 182 | postorder(r.getLeft()); 183 | postorder(r.getRight()); 184 | System.out.print(r.getData() +" "); 185 | } 186 | } 187 | } 188 | 189 | /* Class BinaryTree */ 190 | public class BinaryTree 191 | { 192 | public static void main(String[] args) 193 | { 194 | Scanner scan = new Scanner(System.in); 195 | /* Creating object of BT */ 196 | BT bt = new BT(); 197 | /* Perform tree operations */ 198 | System.out.println("Binary Tree \n"); 199 | char ch; 200 | do 201 | { 202 | System.out.println("\nBinary Tree Operations\n"); 203 | System.out.println("1. insert "); 204 | System.out.println("2. search"); 205 | System.out.println("3. count nodes"); 206 | System.out.println("4. check empty"); 207 | 208 | int choice = scan.nextInt(); 209 | switch (choice) 210 | { 211 | case 1 : 212 | System.out.println("Enter integer element to insert"); 213 | bt.insert( scan.next() ); 214 | break; 215 | case 2 : 216 | System.out.println("Enter integer element to search"); 217 | System.out.println("Search result : "+ bt.search( scan.next() )); 218 | break; 219 | case 3 : 220 | System.out.println("Nodes = "+ bt.countNodes()); 221 | break; 222 | case 4 : 223 | System.out.println("Empty status = "+ bt.isEmpty()); 224 | break; 225 | default : 226 | System.out.println("Wrong Entry \n "); 227 | break; 228 | } 229 | /* Display tree */ 230 | //System.out.print("\nPost order : "); 231 | //bt.postorder(); 232 | //System.out.print("\nPre order : "); 233 | //bt.preorder(); 234 | System.out.print("\nIn order : "); 235 | bt.inorder(); 236 | //System.out.println("Expression print"); 237 | //bt.printExpression(); 238 | 239 | System.out.println("\n\nDo you want to continue (Type y or n) \n"); 240 | ch = scan.next().charAt(0); 241 | } while (ch == 'Y'|| ch == 'y'); 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /DataStructure實作/ListDemo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node { 4 | int data; 5 | struct Node *next; 6 | }; 7 | void showList(struct Node **head){ 8 | struct Node *ptr = *head; 9 | while(ptr != NULL) { 10 | printf("%d \n",ptr -> data); 11 | ptr = ptr -> next; 12 | } 13 | } 14 | int main() { 15 | struct Node *head = NULL; 16 | //head = (struct Node *)malloc(sizeof(struct Node)); 17 | struct Node temp; 18 | struct Node temp2; 19 | head = &temp; 20 | temp.data = 1; 21 | temp2.data = 2; 22 | temp.next = &temp2; 23 | showList(&head); 24 | } 25 | -------------------------------------------------------------------------------- /DataStructure實作/ListDemo.java: -------------------------------------------------------------------------------- 1 | class LinkedList { 2 | public Node head; 3 | public int count; 4 | LinkedList(){ 5 | this.head = new Node(); 6 | this.head.Next = new Node(); 7 | this.count = 1; 8 | } 9 | } 10 | class Node { 11 | public int data; 12 | public Node Next; 13 | public Node() { 14 | this.data = 0; 15 | this.Next = null; 16 | } 17 | public Node(int data) { 18 | this.data = data; 19 | this.Next = null; 20 | } 21 | } 22 | public class ListDemo { 23 | public static void main(String[] argv) { 24 | LinkedList l1 = new LinkedList(); 25 | System.out.println(l1.count); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DataStructure實作/Stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define max 100 5 | int top,stack[max]; 6 | 7 | 8 | void push(char x){ 9 | // Push(Inserting Element in stack) operation 10 | if(top == max - 1){ 11 | printf("stack overflow"); 12 | } else { 13 | stack[++top] = x; 14 | } 15 | } 16 | 17 | void pop(){ 18 | // Pop (Removing element from stack) 19 | printf("%c", stack[top--]); 20 | } 21 | 22 | 23 | int main() 24 | { 25 | 26 | char str[] = "I Love Programming"; 27 | int len = strlen(str), i; 28 | 29 | 30 | for(i=0; i 2 | #include 3 | 4 | /* Create a node which contains data and next pointer link */ 5 | 6 | struct node{ 7 | 8 | int data; 9 | struct node *link; 10 | }; 11 | 12 | struct node *head; 13 | 14 | void push(int data){ 15 | 16 | /* Allocate memory */ 17 | 18 | struct node *newNode = (struct node*)malloc(sizeof(struct node)); 19 | 20 | /* If memory is full */ 21 | 22 | if (newNode == NULL) { 23 | 24 | printf("Stack Overflow \n"); 25 | return; 26 | } 27 | 28 | /* Create new node */ 29 | newNode->data = data; 30 | newNode->link = head; 31 | head = newNode; 32 | } 33 | 34 | void pop() { 35 | /* If there is no element to pop */ 36 | if(head == NULL) { 37 | printf("Stack Underflow"); 38 | exit(0); 39 | } 40 | 41 | struct node *p = head; 42 | 43 | int data; 44 | data = p->data; 45 | 46 | /* Points to next link */ 47 | 48 | head = head->link; 49 | free(p); 50 | 51 | printf("Popped element is %d\n",data); 52 | } 53 | 54 | int main() { 55 | /*Initially head is assigned null. */ 56 | head = NULL; 57 | 58 | push(4); 59 | push(6); 60 | pop(); 61 | push(8); 62 | pop(); 63 | } 64 | -------------------------------------------------------------------------------- /DecimalToOther.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DecimalToOther { 4 | public static void main(String[] argv) { 5 | //10進位轉2進位 6 | //忘了哪間學校 7 | System.out.println(decimalToBinary(47)); 8 | //10進位轉8進位 9 | //中央103資結 10 | System.out.println(decimalToOctal(68)); 11 | } 12 | public static String decimalToBinary(int number) { 13 | String result = ""; 14 | while(number != 0) { 15 | result += number % 2; 16 | number /= 2; 17 | } 18 | //算完要再反轉 19 | return Reverse(result); 20 | } 21 | public static String Reverse(String str) { 22 | return new StringBuilder(str).reverse().toString(); 23 | } 24 | 25 | //運用Stack做反轉[中央103] 26 | public static String decimalToOctal(int number) { 27 | String result = ""; 28 | Stack S = new Stack(); 29 | while(number != 0) { 30 | S.push(number % 8); 31 | number /= 8; 32 | } 33 | while(!S.isEmpty()) { 34 | result += S.pop(); 35 | } 36 | return result; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LCS_DP.java: -------------------------------------------------------------------------------- 1 | public class LCS_DP { 2 | public static void main(String[] argv) { 3 | int[] s1 = {0, 2, 5, 7, 9, 3, 1, 2}; 4 | int[] s2 = {0, 3, 5, 3, 2, 8}; 5 | 6 | int[][] array = new int[s1.length][s2.length]; 7 | /* DP規則 8 | 9 | LCS(s1, s2) = 10 | { max( LCS(sub1, s2), LCS(s1, sub2) ) , when e1 != e2 11 | { LCS(sub1, sub2) + e1 , when e1 == e2 12 | */ 13 | 14 | for(int i = 1; i < s1.length ; i++) { 15 | for(int j = 1; j < s2.length ; j++) { 16 | if (s1[i] == s2[j]) 17 | // +1是因為e1的長度為1 18 | array[i][j] = array[i-1][j-1] + 1; 19 | else 20 | array[i][j] = Math.max(array[i-1][j], array[i][j-1]); 21 | 22 | } 23 | } 24 | System.out.println("LCS的長度是" + array[s1.length - 1][s2.length - 1]); 25 | System.out.println(); 26 | 27 | //印出矩陣 28 | for(int i = 0; i < s1.length; i++) { 29 | for(int j = 0; j < s2.length; j++) { 30 | System.out.print(array[i][j] + " "); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LCS_Recursive.java: -------------------------------------------------------------------------------- 1 | public class LCS_Recursive { 2 | 3 | public static void main(String[] argv) { 4 | char[] X = {'A','G','G','T','A','B'}; 5 | char[] Y = {'G','X','T','X','A','Y','B'}; 6 | 7 | int m = X.length; 8 | int n = Y.length; 9 | System.out.printf("Length of LCS is %d\n", lcs( X, Y, m, n ) ); 10 | } 11 | 12 | public static int lcs(char[] X, char[] Y, int m, int n) 13 | { 14 | if (m == 0 || n == 0) 15 | return 0; 16 | if (X[m-1] == Y[n-1]) 17 | return 1 + lcs(X, Y, m-1, n-1); 18 | else 19 | return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); 20 | } 21 | public static int max(int a, int b) 22 | { 23 | return (a > b)? a : b; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Matrix相關/MatrixMultiply.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yysu/Ds-algo/275c7ea350dddc3addd4418699790df010bc3c33/Matrix相關/MatrixMultiply.c -------------------------------------------------------------------------------- /Matrix相關/MatrixTranspose.java: -------------------------------------------------------------------------------- 1 | public class MatrixTranspose 2 | { 3 | public static void main (String[] args) throws java.lang.Exception 4 | { 5 | // your code goes here 6 | int[][] array = { { 1, 2, 3 } , { 4, 5, 6 } , { 7, 8, 9 } }; 7 | System.out.println("轉置前 : "); 8 | print(array); 9 | 10 | //轉置 11 | for(int i = 0;i < array.length; i++){ 12 | for(int j = i; j < array[0].length; j++){ 13 | int temp = array[i][j]; 14 | array[i][j] = array[j][i]; 15 | array[j][i] = temp; 16 | } 17 | } 18 | 19 | System.out.println("轉置後 : "); 20 | print(array); 21 | } 22 | public static void print(int[][] array) { 23 | // print 二維矩陣 24 | for(int i = 0; i < array.length; i++) { 25 | for(int j = 0; j < array[0].length; j++) 26 | System.out.print(array[i][j] + " "); 27 | System.out.println(); 28 | } 29 | System.out.println(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /NCCU-DCT/Caesar.js: -------------------------------------------------------------------------------- 1 | //政大105數位 資結考題 2 | //https://yysu.github.io/jsplayground/ 3 | function GetASCII(Str) { 4 | return Str.charCodeAt(0); 5 | } 6 | 7 | function Sub(Str) { 8 | if(Str == 32) 9 | return Str; 10 | else 11 | return Str-3; 12 | } 13 | 14 | function ToChar(Str) { 15 | return String.fromCharCode(Str); 16 | } 17 | 18 | var plainText = "DOT NOT PIG"; 19 | var plainTextArray = plainText.split(""); 20 | var cipherTextArray = plainTextArray.map(GetASCII).map(Sub).map(ToChar); 21 | var cipherText = cipherTextArray.join(""); 22 | console.log(cipherText); 23 | -------------------------------------------------------------------------------- /NCCU-DCT/NCCU106.java: -------------------------------------------------------------------------------- 1 | //政大數位106 考題 2 | //忘記哪一題了 再請發pull request給我 3 | public class NCCU106 { 4 | public static void main(String[] argv) { 5 | long n = 123456789; 6 | System.out.println(g(n)); 7 | } 8 | public static int g(long n) { 9 | int num = 0; 10 | String str = String.valueOf(n); 11 | while(str.length() > 1) { 12 | for(int i = 0; i < str.length(); i++) { 13 | num += Integer.parseInt(String.valueOf(str.charAt(i))); 14 | } 15 | if(num > 10) { 16 | str = String.valueOf(num); 17 | num = 0; 18 | } else { 19 | break; 20 | } 21 | } 22 | return num; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /NCCU-DCT/canvas.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Canvas 7 | 8 | 9 |
10 |

Canvas

11 | 12 | 22 | 23 |
24 | 25 | -------------------------------------------------------------------------------- /NCCU/100/計概/Two.java: -------------------------------------------------------------------------------- 1 | public class Two { 2 | public static void main(String[] argv) { 3 | //array A 4 | int[] A = {1, 2, 3, 4, 5, 6}; 5 | System.out.println(IterativeSum(A, 0, A.length)); 6 | System.out.println(BinarySum(A, 0, A.length)); 7 | 8 | } 9 | // 1.1 10 | public static int IterativeSum(int[] A, int i, int n) { 11 | int sum = 0; 12 | for (int index = i; index < i + n; index++) { 13 | sum += A[index]; 14 | } 15 | return sum; 16 | } 17 | // 1.2 18 | public static int BinarySum(int[] A, int i, int n) { 19 | if(n == 1) { 20 | return A[i]; 21 | } 22 | else if (n == 2) { 23 | return A[i] + A[i + 1]; 24 | } 25 | else { 26 | int left, right; 27 | if( n % 2 == 0) { 28 | //陣列是偶數個的處理 29 | left = BinarySum(A, i, n/2); 30 | right = BinarySum(A, i + n/2, n/2); 31 | return left + right; 32 | } 33 | else { 34 | //陣列是奇數個的處理 35 | left = BinarySum(A, i, n/2); 36 | right = BinarySum(A, i + n/2 , n/2 + 1); 37 | return left + right; 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NCU/100/計概/Test.java: -------------------------------------------------------------------------------- 1 | class Employee { 2 | //Section A : 宣告變數 3 | public Employee boss; 4 | public String ID; 5 | public double total_bonus; 6 | 7 | //Section B 8 | public Employee(String ID) { 9 | total_bonus = 0.0; 10 | this.ID = ID; 11 | } 12 | public Employee(String ID, Employee boss) { 13 | total_bonus = 0.0; 14 | this.ID = ID; 15 | this.boss = boss; 16 | } 17 | 18 | //Section C : 在此計算銷售線上各層人員的分紅 19 | public void addBonus(double value) { 20 | total_bonus += value * 0.1; 21 | if(this.boss != null) 22 | this.boss.addBonus(value * 0.1); 23 | } 24 | 25 | //Section D 26 | public double getBonus() { 27 | return total_bonus; 28 | } 29 | public String toString() { 30 | return ID; 31 | } 32 | } 33 | public class Test { 34 | public static void main(String[] argv) { 35 | //Section E 36 | Employee a1 = new Employee("001"); 37 | Employee a2 = new Employee("002", a1); 38 | Employee a3 = new Employee("003", a2); 39 | Employee a4 = new Employee("004", a3); 40 | 41 | //Section F 42 | a4.addBonus(10000); 43 | a2.addBonus(20000); 44 | 45 | //Section G 46 | System.out.println(a1 + ":" + a1.getBonus() + "\n" + 47 | a2 + ":" + a2.getBonus() + "\n" + 48 | a3 + ":" + a3.getBonus() + "\n" + 49 | a4 + ":" + a4.getBonus() + "\n" 50 | ); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /NCU/102/計概/Question1Test.java: -------------------------------------------------------------------------------- 1 | class Question1 { 2 | int a = 5; 3 | public void methodA() { 4 | int a = 7; 5 | a = a + 1; 6 | this.a = a + 2; 7 | System.out.print(a); 8 | } 9 | } 10 | 11 | public class Question1Test { 12 | public static void main(String[] args) { 13 | Question1 aa = new Question1(); 14 | aa.methodA(); 15 | System.out.print(aa.a); 16 | } 17 | } -------------------------------------------------------------------------------- /NCU/102/計概/Question2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | interface B { 6 | public void c(); 7 | } 8 | 9 | abstract class A implements B { 10 | public void A(){System.out.print("Ω");} 11 | public abstract void a(); //correct 12 | public final void f() {System.out.print("⌃");} //correct 13 | public void g() {System.out.print("⍦");} 14 | public final double zz; 15 | } 16 | 17 | class C extends A //correct 18 | { 19 | public C() { 20 | super(); 21 | System.out.print("Ω"); 22 | } 23 | public void a() { 24 | System.out.print("⍬"); 25 | } 26 | public void c() { 27 | System.out.print("u"); 28 | } 29 | public void d() { 30 | System.out.print("0"); 31 | } 32 | public void f() { 33 | System.out.print("w"); 34 | } 35 | public void g() { 36 | System.out.print("II"); 37 | } 38 | } 39 | 40 | 41 | class Question2 { 42 | public static void main (String[] args) throws java.lang.Exception 43 | { 44 | // your code goes here 45 | A a1 = new C(); 46 | C c1 = new C(); 47 | c1 = a1; 48 | c1.d(); 49 | a1.g(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /NCU/102/計概/Question2_Solve.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | interface B { 6 | public void c(); 7 | } 8 | 9 | abstract class A implements B { 10 | public void A(){System.out.print("Ω");} 11 | public abstract void a(); //correct 12 | public final void f() {System.out.print("⌃");} //correct 13 | public void g() {System.out.print("⍦");} 14 | public final double zz = 0.0; 15 | } 16 | 17 | class C extends A //correct 18 | { 19 | public C() { 20 | super(); 21 | System.out.print("Ω"); 22 | } 23 | public void a() { 24 | System.out.print("⍬"); 25 | } 26 | public void c() { 27 | System.out.print("u"); 28 | } 29 | public void d() { 30 | System.out.print("0"); 31 | } 32 | /* 33 | public void f() { 34 | System.out.print("w"); 35 | }*/ 36 | public void g() { 37 | System.out.print("II"); 38 | } 39 | } 40 | 41 | 42 | class Question2_Solve { 43 | public static void main (String[] args) throws java.lang.Exception 44 | { 45 | // your code goes here 46 | A a1 = new C(); 47 | C c1 = new C(); 48 | c1 = (C)a1; 49 | c1.d(); 50 | a1.g(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /NCU/104/計概/Night.java: -------------------------------------------------------------------------------- 1 | interface A { 2 | public int ma(int a); 3 | public int mb(int a); 4 | } 5 | 6 | abstract class B implements A { 7 | public B(int a) { 8 | System.out.println(ma(a)); 9 | } 10 | @Override 11 | public int ma(int a) { 12 | int sum = 0; 13 | for (int i = 1; i <= a; i++) { 14 | sum += i; 15 | } 16 | return sum; 17 | } 18 | } 19 | 20 | class C extends B { 21 | public C(int a) { 22 | super(a); 23 | } 24 | @Override 25 | public int mb(int a) { 26 | int ans = 1; 27 | for (int i = 1; i <= a; i++) { 28 | ans *= i; 29 | } 30 | return ans; 31 | } 32 | } 33 | 34 | public class Night { 35 | public static void main(String[] argv) { 36 | C c = new C(5); 37 | System.out.print(c.mb(c.ma(4))); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /NCU/106/計概/Eight.java: -------------------------------------------------------------------------------- 1 | public class Eight { 2 | public static void main(String[] argv) { 3 | // 假設連續整數範圍3-9 4 | int[] array = {3, 6, 9, 7, 8, 4}; 5 | System.out.print(solution(array, array.length)); 6 | } 7 | // space complexity O(1)的方法 8 | public static int solution(int[] array, int n) { 9 | // 宣告一個整數Sum來去存陣列裡面所有數字相加的結果,過程中找出最大及最小值 10 | // 最後由(最大+最小)*(連續整數個數)/2 - Sum 即為答案 11 | int sum = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; 12 | for (int i = 0; i < n; i++) { 13 | sum += array[i]; 14 | if (array[i] < min) { 15 | min = array[i]; 16 | } 17 | if (array[i] > max) { 18 | max = array[i]; 19 | } 20 | } 21 | int ans = (max + min) * (n + 1) / 2 - sum; 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /NSYSU/106/TwoStringCombine.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | // your code goes here 5 | char str1[3] = {'a','b','c'}; 6 | char str2[3] = {'a','b','c'}; 7 | int len1 = sizeof(str1)/sizeof(char); 8 | int len2 = sizeof(str2)/sizeof(char); 9 | 10 | 11 | char output[len1+len2]; 12 | 13 | for(int i =0; i< len1; i++) 14 | output[i] = str1[i]; 15 | 16 | int index = 0; 17 | 18 | for(int i = len1; i< len1 + len2; i++) 19 | output[i] = str2[index++]; 20 | 21 | for(int i =0; i 2 | #include 3 | int Binsearch (int data[], int l, int u, int key); 4 | 5 | int main(void) { 6 | // your code goes here 7 | int data[5] = {1,2,3,4,5}; 8 | printf("%d", Binsearch(data, 0, 5, 4)); 9 | 10 | return 0; 11 | } 12 | int Binsearch (int data[], int l, int u, int key) { 13 | if(l <= u) { 14 | int m = (l + u) / 2; 15 | if ( data[m] > key ) { 16 | return Binsearch(data, l, m-1, key); 17 | } else if (data[m] < key) { 18 | return Binsearch(data, m+1, u, key); 19 | } else { 20 | return m; 21 | } 22 | } else { 23 | return -1; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Search/Binarysearch.java: -------------------------------------------------------------------------------- 1 | public class Binarysearch { 2 | //imperative版本 3 | public static void main(String[] argv) { 4 | int[] array = { 1, 2, 3, 4, 5 }; 5 | int key = 4; 6 | if(Binsearch(array, key, array.length) != -1) { 7 | System.out.println("找到" + key + "在 : " + Binsearch(array, key, array.length)); 8 | } else { 9 | System.out.println("找不到"); 10 | } 11 | } 12 | public static int Binsearch (int[] array, int key, int n) { 13 | int m,l = 0, u = n - 1; 14 | while(l <= u) { 15 | m = (l + u) / 2; 16 | if(key > array[m]) { 17 | l = m + 1; 18 | } else if(key < array[m]) { 19 | u = m - 1; 20 | } else { 21 | return m; 22 | } 23 | } 24 | return -1; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Sort/BubbleSort.java: -------------------------------------------------------------------------------- 1 | public class BubbleSort { 2 | public static void main(String[] argv) { 3 | int[] array = { 20, 3, 4, 7, 15, 6, 2, 100, 1000, 32 }; 4 | System.out.println("排序前:"); 5 | print(array); 6 | System.out.println(); 7 | bubble(array, array.length); 8 | System.out.println("排序後:"); 9 | print(array); 10 | } 11 | public static void bubble(int[] array, int n) { 12 | for(int i = 0; i < n - 1; i++) { 13 | for(int j = 0; j < n - i - 1; j++) { 14 | if (array[j] > array[j + 1]) { 15 | int temp = array[j]; 16 | array[j] = array[j + 1]; 17 | array[j + 1] = temp; 18 | } 19 | } 20 | } 21 | } 22 | public static void print(int[] array) { 23 | for(int i = 0; i < array.length; i++) { 24 | System.out.print(array[i] + " "); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Sort/CountingSort.java: -------------------------------------------------------------------------------- 1 | //https://www.youtube.com/watch?v=7zuGmKfUt7s 2 | public class CountingSort { 3 | public static void main(String[] argv) { 4 | int[] array = { 1,4,1,2,7,5,2 }; 5 | int[] Count = new int[10]; 6 | int[] Sort = new int[array.length]; 7 | 8 | //Counting sort start 9 | //統計結果 10 | for (int i = 0; i < array.length; i++) { 11 | Count[array[i]]++; 12 | } 13 | print(Count, array.length); 14 | 15 | //算每個數字起始位置 16 | for(int i = 0; i < 9; i++) { 17 | Count[i + 1] += Count[i]; 18 | } 19 | print(Count, 10); 20 | 21 | //填入數字 22 | for (int i = 0; i < array.length; i++) { 23 | int temp = Count[array[i]] - 1; 24 | Sort[temp] = array[i]; 25 | Count[array[i]]--; 26 | } 27 | print(Sort, Sort.length); 28 | 29 | } 30 | public static void print(int[] array, int n) { 31 | for (int i = 0; i < n; i++) { 32 | System.out.print(array[i] + " "); 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Sort/InsertionSort.java: -------------------------------------------------------------------------------- 1 | public class InsertionSort { 2 | public static void main(String[] argv) { 3 | int[] array = { -10000000, 26,5,37,1,61,11,59,15,48,19}; 4 | //排序前 5 | System.out.println("Before sort"); 6 | print(array); 7 | System.out.println(); 8 | 9 | Insort(array, array.length); 10 | //排序後 11 | System.out.println("After sort"); 12 | print(array); 13 | } 14 | public static void Insert(int[] array, int r, int index) { 15 | while(r < array[index]) { 16 | array[index + 1] = array[index]; 17 | index--; 18 | } 19 | array[index + 1] = r; 20 | print(array); 21 | System.out.println(); 22 | 23 | } 24 | public static void Insort(int[] array, int n) { 25 | for(int i = 2;i < n; i++) { 26 | Insert(array, array[i], i - 1); 27 | } 28 | } 29 | //print array function 30 | public static void print(int[] array) { 31 | for(int i = 1;i < array.length; i++) { 32 | System.out.print(array[i] + " "); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sort/MergeSort.java: -------------------------------------------------------------------------------- 1 | public class MergeSort 2 | { 3 | static int count = 1; 4 | public static void MergetoOne(int [] L, int [] R, int [] A) 5 | { 6 | //Please implement this function yourself 7 | int left = 0, right = 0, index = 0; 8 | while(left < L.length && right < R.length) { 9 | if(L[left] < R[right]) { 10 | A[index] = L[left]; 11 | index++; 12 | left++; 13 | } else { 14 | A[index] = R[right]; 15 | index++; 16 | right++; 17 | } 18 | } 19 | while (left < L.length) { 20 | A[index] = L[left]; 21 | index++; 22 | left++; 23 | } 24 | while (right < R.length) { 25 | A[index] = R[right]; 26 | index++; 27 | right++; 28 | } 29 | } 30 | public static void MergeSort(int [] A) 31 | { 32 | int n = A.length; 33 | if (n < 2) 34 | return; 35 | int mid = n / 2; 36 | int [] left = new int[mid]; 37 | int[] right = new int[n - mid]; 38 | for (int i = 0; i < mid; i++) 39 | left[i] = A[i]; 40 | for (int i = mid; i < n; i++) 41 | right[i-mid] = A[i]; 42 | 43 | MergeSort(left); 44 | MergeSort(right); 45 | MergetoOne(left, right, A); 46 | System.out.println("sort count = " + count++ + " "); 47 | for (int i = 0; i < A.length; i++) 48 | System.out.print(" " + A[i] + " "); 49 | System.out.println(); 50 | } 51 | public static void main(String[] args) 52 | { 53 | int[] data = new int[] { 2, 4, 1, 6, 8, 5, 3, 7 }; 54 | System.out.println("排序前:"); 55 | for (int i = 0; i < data.length; i++) 56 | System.out.print(" " + data[i] + " "); 57 | System.out.println(); 58 | // 59 | MergeSort(data); 60 | // 61 | System.out.println("排序後:"); 62 | for (int i = 0; i < data.length; i++) 63 | System.out.print(" " + data[i] + " "); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Sort/QuickPartitionAlgo.java: -------------------------------------------------------------------------------- 1 | public class QuickPartitionAlgo { 2 | public static void main(String[] argv) { 3 | int []A = { 9, 4, 2, 3, 6, 5, 7 }; 4 | int smallesti = 3; 5 | //排序前 6 | print(A); 7 | 8 | //Find ith Smallest number 9 | System.out.print("第" + smallesti + "小的數字為: "); 10 | System.out.println(findSmallest(A, 0, A.length - 1, smallesti)); 11 | 12 | QuickSort(A, 0, A.length - 1); 13 | //排序後 14 | print(A); 15 | } 16 | public static int findSmallest(int[] A, int p, int r, int i) { 17 | //Time Complexity : O(n) 18 | int q = Partition(A, p, r); 19 | // q = pk為第 k 小的數 20 | int k = q - p + 1; 21 | if(i == k) { 22 | return A[q]; 23 | } else if (i < k) { 24 | return findSmallest(A, p, q - 1, i); 25 | } else { 26 | return findSmallest(A, q + 1, r, i - k); 27 | } 28 | 29 | } 30 | public static void QuickSort(int[] A, int p, int r) { 31 | if(p < r) { 32 | int q = Partition(A, p, r); 33 | QuickSort(A, p, q - 1); 34 | QuickSort(A, q + 1, r); 35 | } 36 | } 37 | public static int Partition(int[] A, int p, int r) { 38 | int pk = A[r]; 39 | int i = p - 1; 40 | for (int j = p; j <= r - 1; j++) { 41 | if(A[j] <= pk) { 42 | i++; 43 | //Swap(A[i], A[j]); 44 | int temp = A[i]; 45 | A[i] = A[j]; 46 | A[j] = temp; 47 | } 48 | } 49 | //Swap(A[r], A[i + 1]); 50 | int temp = A[r]; 51 | A[r] = A[i + 1]; 52 | A[i + 1] = temp; 53 | return i + 1; 54 | } 55 | public static void print(int[] A) { 56 | for (int index : A) 57 | System.out.print(index + " "); 58 | System.out.println(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Sort/QuickSort.java: -------------------------------------------------------------------------------- 1 | public class QuickSort { 2 | public static void main(String[] argv) { 3 | int[] array = { 20, 3, 4, 7, 15, 6, 2, 100, 1000, 32 }; 4 | System.out.println("排序前:"); 5 | print(array); 6 | System.out.println(); 7 | quick(array, 0, array.length - 1); 8 | System.out.println("排序後:"); 9 | print(array); 10 | } 11 | public static void quick(int[] array, int L, int U) { 12 | if(L < U) { 13 | int pivot = array[L]; 14 | int i = L; 15 | int j = U + 1; 16 | while(i < j) { 17 | do { 18 | i++; 19 | } while(pivot > array[i]); 20 | do { 21 | j--; 22 | } while(pivot < array[j]); 23 | 24 | if(i < j) { 25 | int temp = array[i]; 26 | array[i] = array[j]; 27 | array[j] = temp; 28 | } 29 | } 30 | int temp = array[L]; 31 | array[L] = array[j]; 32 | array[j] = temp; 33 | quick(array, L , j-1); 34 | quick(array, j + 1, U); 35 | } 36 | } 37 | public static void print(int[] array) { 38 | for(int i = 0; i < array.length; i++) { 39 | System.out.print(array[i] + " "); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Sort/SelectionSort.java: -------------------------------------------------------------------------------- 1 | public class SelectionSort { 2 | public static void main(String[] argv) { 3 | int[] array = { 26, 5, 37, 1, 61, 11, 59, 15, 48, 19 }; 4 | System.out.println("排序前:"); 5 | print(array); 6 | System.out.println(); 7 | Selection(array, array.length); 8 | System.out.println("排序後:"); 9 | print(array); 10 | } 11 | public static void Selection(int[] array, int n) { 12 | int min; 13 | for(int i = 0; i < n - 1; i++) { 14 | min = i; 15 | for(int j = i + 1; j < n; j++) { 16 | if (array[j] < array[min]) { 17 | min = j; 18 | } 19 | } 20 | if (min != i) { 21 | int temp = array[i]; 22 | array[i] = array[min]; 23 | array[min] = temp; 24 | } 25 | System.out.print("第" + (i+1) + "回合結果"); 26 | print(array); 27 | System.out.println(); 28 | } 29 | } 30 | public static void print(int[] array) { 31 | for(int i = 0; i < array.length; i++) { 32 | System.out.print(array[i] + " "); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /StringRecursiveReverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | void reverse(char *str) { 3 | if(*str) { 4 | reverse(str+1); 5 | printf("%c", *str); 6 | } 7 | } 8 | int main(void) { 9 | // your code goes here 10 | char a[]= "Geeks for geek"; 11 | reverse(a); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /StringReverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void reverse(char* str, int n) { 5 | if(n < 0) { 6 | } else { 7 | printf("%c", str[n]); 8 | n--; 9 | reverse(str,n); 10 | } 11 | } 12 | int main() { 13 | // your code goes here 14 | char str[6] ="string"; 15 | int n = strlen(str); 16 | reverse(str, n-1); 17 | return 0; 18 | } 19 | --------------------------------------------------------------------------------