提交 f7fbf06f 编写于 作者: W wizardforcel

2020-07-10 23:39:40

上级 2238acf0
......@@ -61,7 +61,7 @@ FIFO 表示
```
```py
# Queue implementation in Python
class Queue:
......@@ -101,7 +101,7 @@ print("After removing an element")
q.display()
```
```
```java
// Queue implementation in Java
public class Queue {
......@@ -202,7 +202,7 @@ public class Queue {
}
```
```
```c
// Queue implementation in C
#include <stdio.h>
......@@ -276,7 +276,7 @@ void display() {
}
```
```
```cpp
// Queue implementation in C++
#include <iostream>
......
......@@ -66,7 +66,7 @@ if REAR + 1 == 5 (overflow!), REAR = (REAR + 1)%5 = 0 (start of queue)
```
```py
# Circular Queue implementation in Python
class MyCircularQueue(object):
......@@ -149,7 +149,7 @@ obj.deQueue()
obj.Display()
```
```
```java
// Circular Queue implementation in Java
public class CQueue {
......@@ -264,7 +264,7 @@ public class CQueue {
}
```
```
```c
// Circular Queue implementation in C
#include <stdio.h>
......@@ -364,7 +364,7 @@ int main() {
}
```
```
```cpp
// Circular Queue implementation in C++
#include <iostream>
......
......@@ -155,7 +155,7 @@ return rootNode
```
```py
# Max-Heap data structure in Python
# Function to heapify the tree
......@@ -215,7 +215,7 @@ deleteNode(arr, 4)
print("After deleting an element: " + str(arr))
```
```
```java
// Max-Heap data structure in Java
import java.util.ArrayList;
......@@ -306,7 +306,7 @@ class Heap {
}
```
```
```c
// Max-Heap data structure in C
#include <stdio.h>
......@@ -396,7 +396,7 @@ int main() {
}
```
```
```cpp
// Max-Heap data structure in C++
#include <iostream>
......
......@@ -158,7 +158,7 @@
```
```py
# Deque operations in python
class Deque:
......@@ -199,7 +199,7 @@ d.addRear(45)
print(d.items)
```
```
```java
// Deque operations in Java
class Deque {
......@@ -340,7 +340,7 @@ class Deque {
}
```
```
```c
// Deque operations in C
#include <stdio.h>
......@@ -529,7 +529,7 @@ int count(int *arr)
}
```
```
```cpp
// Deque operations in C++
#include <iostream>
......
......@@ -106,7 +106,7 @@ head = one;
```
```py
# Linked list implementation in Python
class Node:
......@@ -139,7 +139,7 @@ if __name__ == '__main__':
linked_list.head = linked_list.head.next
```
```
```java
// Linked list implementation in Java
class LinkedList {
......@@ -177,7 +177,7 @@ class LinkedList {
}
```
```
```c
// Linked list implementation in C
#include <stdio.h>
......@@ -225,7 +225,7 @@ int main() {
}
```
```
```cpp
// Linked list implementation in C++
#include <bits/stdc++.h>
......
......@@ -158,7 +158,7 @@ temp->next = temp->next->next;
```
```py
# Linked list operations in Python
# Create a node
......@@ -257,7 +257,7 @@ if __name__ == '__main__':
llist.printList()
```
```
```java
// Linked list operations in Java
class LinkedList {
......@@ -360,7 +360,7 @@ class LinkedList {
}
```
```
```c
// Linked list operations in C
#include <stdio.h>
......@@ -466,7 +466,7 @@ int main() {
}
```
```
```cpp
// Linked list operations in C++
#include <stdlib.h>
......
......@@ -95,9 +95,9 @@ chainedHashDelete(T, x)
## Python,Java,C 和 C++ 实现
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Python program to demonstrate working of HashTable
hashTable = [[],] * 10
......@@ -145,7 +145,7 @@ removeData(123)
print(hashTable)
```
```
```java
// Java program to demonstrate working of HashTable
import java.util.*;
......@@ -168,7 +168,7 @@ class HashTable {
}
```
```
```c
// Implementing hash table in C
#include <stdio.h>
......@@ -342,7 +342,7 @@ int main()
}
```
```
```cpp
// Implementing hash table in C++
#include <iostream>
......
......@@ -143,9 +143,9 @@ return rootNode
## Python,Java,C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Max-Heap data structure in Python
def heapify(arr, n, i):
......@@ -200,7 +200,7 @@ deleteNode(arr, 4)
print("After deleting an element: " + str(arr))
```
```
```java
// Max-Heap data structure in Java
import java.util.ArrayList;
......@@ -287,7 +287,7 @@ class Heap {
}
```
```
```c
// Max-Heap data structure in C
#include <stdio.h>
......@@ -380,7 +380,7 @@ int main()
}
```
```
```cpp
// Max-Heap data structure in C++
#include <iostream>
......
......@@ -208,7 +208,7 @@ insert(H, x)
```
```py
# Fibonacci Heap in python
import math
......@@ -301,7 +301,7 @@ print('the minimum value of the fibonacci heap: {}'.format(fibonacci_heap.get_mi
print('the minimum value removed: {}'.format(fibonacci_heap.extract_min()))
```
```
```java
// Operations on Fibonacci Heap in Java
// Node creation
......@@ -664,7 +664,7 @@ public class fibHeap {
}
```
```
```c
// Operations on a Fibonacci heap in C
#include <math.h>
......@@ -1124,7 +1124,7 @@ int main(int argc, char **argv) {
}
```
```
```cpp
// Operations on a Fibonacci heap in C++
#include <cmath>
......
......@@ -107,7 +107,7 @@
```
```py
# Fibonacci Heap in python
import math
......@@ -194,7 +194,7 @@ print('Minimum value: {}'.format(fheap.get_min()))
print('Minimum value removed: {}'.format(fheap.extract_min()))
```
```
```java
// Operations on Fibonacci Heap in Java
class node {
......@@ -551,7 +551,7 @@ public class fibHeap {
}
```
```
```c
// Operations on a Fibonacci heap in C
#include <stdio.h>
......@@ -1078,7 +1078,7 @@ int main(int argc, char **argv)
}
```
```
```cpp
// Operations on a Fibonacci heap in C++
#include <iostream>
......
......@@ -151,7 +151,7 @@ Left subtree -> root -> right subtree
```
```py
# Tree traversal in Python
class Node:
......@@ -206,7 +206,7 @@ print("\nPostorder traversal ")
postorder(root)
```
```
```java
// Tree traversal in Java
class Node {
......@@ -283,7 +283,7 @@ class BinaryTree {
}
```
```
```c
// Tree traversal in C
#include <stdio.h>
......@@ -360,7 +360,7 @@ int main() {
}
```
```
```cpp
// Tree traversal in C++
#include <iostream>
......
......@@ -117,7 +117,7 @@ struct node
```
```py
# Binary Tree in Python
class Node:
......@@ -165,7 +165,7 @@ print("\nPost order Traversal: ", end="")
root.traversePostOrder()
```
```
```java
// Binary Tree in Java
// Node creation
......@@ -235,7 +235,7 @@ class BinaryTree {
}
```
```
```c
// Tree traversal in C
#include <stdio.h>
......@@ -310,7 +310,7 @@ int main() {
}
```
```
```cpp
// Binary Tree in C++
#include <stdlib.h>
......
......@@ -41,7 +41,7 @@ Let, i = the number of internal nodes
```
```py
# Checking if a binary tree is a full binary tree in Python
# Creating a node
......@@ -83,7 +83,7 @@ else:
print("The tree is not a full binary full")
```
```
```java
// Checking if a binary tree is a full binary tree in Java
class Node {
......@@ -134,7 +134,7 @@ class BinaryTree {
}
```
```
```c
// Checking if a binary tree is a full binary tree in C
#include <stdbool.h>
......@@ -187,7 +187,7 @@ int main() {
}
```
```
```cpp
// Checking if a binary tree is a full binary tree in C++
#include <iostream>
......
......@@ -31,9 +31,9 @@
以下代码用于检查树是否是完美二叉树。
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Checking if a binary tree is a perfect binary tree in Python
class newNode:
......@@ -79,7 +79,7 @@ else:
print("The tree is not a perfect binary tree")
```
```
```java
// Checking if a binary tree is a perfect binary tree in Java
class PerfectBinaryTree {
......@@ -147,7 +147,7 @@ class PerfectBinaryTree {
}
```
```
```c
// Checking if a binary tree is a perfect binary tree in C
#include <stdbool.h>
......@@ -219,7 +219,7 @@ int main() {
}
```
```
```cpp
// Checking if a binary tree is a full binary tree in C++
#include <iostream>
......
......@@ -78,9 +78,9 @@
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Checking if a binary tree is a complete binary tree in C
class Node:
......@@ -125,7 +125,7 @@ else:
print("The tree is not a complete binary tree")
```
```
```java
// Checking if a binary tree is a complete binary tree in Java
// Node creation
......@@ -184,7 +184,7 @@ class BinaryTree {
}
```
```
```c
// Checking if a binary tree is a complete binary tree in C
#include <stdbool.h>
......@@ -242,7 +242,7 @@ int main() {
}
```
```
```cpp
// Checking if a binary tree is a complete binary tree in C++
#include <iostream>
......
......@@ -30,9 +30,9 @@
以下代码用于检查树是否高度平衡。
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Checking if a binary tree is CalculateHeight balanced in Python
# CreateNode creation
......@@ -81,7 +81,7 @@ else:
print('The tree is not balanced')
```
```
```java
// Checking if a binary tree is height balanced in Java
// Node creation
......@@ -146,7 +146,7 @@ class BinaryTree {
}
```
```
```c
// Checking if a binary tree is height balanced in C
#include <stdio.h>
......@@ -209,7 +209,7 @@ int main() {
}
```
```
```cpp
// Checking if a binary tree is height balanced in C++
#include <iostream>
......
......@@ -218,9 +218,9 @@ return node;
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Binary Search Tree operations in Python
# Create a node
......@@ -321,7 +321,7 @@ print("Inorder traversal: ", end=' ')
inorder(root)
```
```
```java
// Binary Search Tree operations in Java
class BinarySearchTree {
......@@ -441,7 +441,7 @@ class BinarySearchTree {
}
```
```
```c
// Binary Search Tree operations in C
#include <stdio.h>
......@@ -556,7 +556,7 @@ int main() {
}
```
```
```cpp
// Binary Search Tree operations in C++
#include <iostream>
......
......@@ -315,9 +315,9 @@ Avl tree
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# AVL tree implementation in Python
import sys
......@@ -485,7 +485,7 @@ print("After Deletion: ")
myTree.printHelper(root, "", True)
```
```
```java
// AVL tree implementation in Java
// Create node
......@@ -680,7 +680,7 @@ class AVLTree {
}
```
```
```c
// AVL tree implementation in C
#include <stdio.h>
......@@ -868,7 +868,7 @@ int main() {
}
```
```
```cpp
// AVL tree implementation in C++
#include <iostream>
......
......@@ -129,9 +129,9 @@ else
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Searching a key on a B-tree in Python
# Create node
......@@ -233,7 +233,7 @@ if __name__ == '__main__':
main()
```
```
```java
// Searching a key on a B-tree in Java
public class BTree {
......@@ -404,7 +404,7 @@ public class BTree {
}
```
```
```c
// Searching a key on a B-tree in C
#include <stdio.h>
......@@ -572,7 +572,7 @@ int main() {
}
```
```
```cpp
// Searching a key on a B-tree in C++
#include <iostream>
......
......@@ -92,9 +92,9 @@ n[x] = n[x] + 1
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Inserting a key on a B-tree in Python
# Create a node
......@@ -177,7 +177,7 @@ if __name__ == '__main__':
main()
```
```
```java
// Inserting a key on a B-tree in Java
public class BTree {
......@@ -312,7 +312,7 @@ public class BTree {
}
```
```
```c
// insertioning a key on a B-tree in C
#include <stdio.h>
......@@ -561,7 +561,7 @@ int main() {
}
```
```
```cpp
// Inserting a key on a B-tree in C++
#include <iostream>
......
......@@ -105,9 +105,9 @@ B 树中的删除操作主要有三种情况。
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Deleting a key on a B-tree in Python
# Btree node
......@@ -316,7 +316,7 @@ def main():
B.print_tree(B.root)
```
```
```java
// Inserting a key on a B-tree in Java
import java.util.Stack;
......@@ -692,7 +692,7 @@ public class BTree {
}
```
```
```c
// Deleting a key from a B-tree in C
#include <stdio.h>
......@@ -1029,7 +1029,7 @@ int main() {
}
```
```
```cpp
// Deleting a key from a B-tree in C++
#include <iostream>
......
......@@ -110,9 +110,9 @@ B+ 树
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# B+ tee in python
import math
......@@ -427,7 +427,7 @@ else:
print("Not found")
```
```
```java
// Searching on a B+ tree in Java
import java.util.*;
......@@ -1007,7 +1007,7 @@ public class BPlusTree {
}
```
```
```c
// Searching on a B+ Tree in C
#include <stdbool.h>
......@@ -1598,7 +1598,7 @@ int main() {
}
```
```
```cpp
// Searching on a B+ tree in C++
#include <climits>
......
......@@ -86,9 +86,9 @@
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# B+ tee in python
import math
......@@ -253,7 +253,7 @@ else:
print("Not found")
```
```
```java
// Searching on a B+ tree in Java
import java.util.*;
......@@ -833,7 +833,7 @@ public class BPlusTree {
}
```
```
```c
// Searching on a B+ Tree in C
#include <stdbool.h>
......@@ -1424,7 +1424,7 @@ int main() {
}
```
```
```cpp
// Searching on a B+ tree in C++
#include <climits>
......
......@@ -72,9 +72,9 @@
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# B+ tee in python
import math
......@@ -389,7 +389,7 @@ else:
print("Not found")
```
```
```java
// Searching on a B+ tree in Java
import java.util.*;
......@@ -968,7 +968,7 @@ public class BPlusTree {
}
```
```
```c
// Searching on a B+ Tree in C
#include <stdbool.h>
......@@ -1559,7 +1559,7 @@ int main() {
}
```
```
```cpp
// Searching on a B+ tree in C++
#include <climits>
......
......@@ -318,9 +318,9 @@
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Implementing Red-Black Tree in Python
import sys
......@@ -662,7 +662,7 @@ if __name__ == "__main__":
bst.print_tree()
```
```
```java
// Implementing Red-Black Tree in Java
class Node {
......@@ -1072,7 +1072,7 @@ public class RedBlackTree {
}
```
```
```c
// Implementing Red-Black Tree in C
#include <stdio.h>
......@@ -1425,7 +1425,7 @@ int main() {
}
```
```
```cpp
// Implementing Red-Black Tree in C++
#include <iostream>
......
......@@ -164,9 +164,9 @@
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Implementing Red-Black Tree in Python
import sys
......@@ -399,7 +399,7 @@ if __name__ == "__main__":
bst.print_tree()
```
```
```java
// Implementing Red-Black Tree in Java
class Node {
......@@ -752,7 +752,7 @@ public class RedBlackTree {
}
```
```
```c
// Implementing Red-Black Tree in C
#include <stdio.h>
......@@ -1105,7 +1105,7 @@ int main() {
}
```
```
```cpp
// Implementing Red-Black Tree in C++
#include <iostream>
......
......@@ -191,9 +191,9 @@
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Implementing Red-Black Tree in Python
import sys
......@@ -535,7 +535,7 @@ if __name__ == "__main__":
bst.print_tree()
```
```
```java
// Implementing Red-Black Tree in Java
class Node {
......@@ -945,7 +945,7 @@ public class RedBlackTree {
}
```
```
```c
// Implementing Red-Black Tree in C
#include <stdio.h>
......@@ -1298,7 +1298,7 @@ int main() {
}
```
```
```cpp
// Implementing Red-Black Tree in C++
#include <iostream>
......
......@@ -112,9 +112,9 @@ Kosaraju 算法基于[两次实现的深度优先搜索算法](/dsa/graph-dfs)
## Python,Java,C++ 示例
[Python](#python-code)[Java](#java-code)[C++](#cpp-code)
```
```py
# Kosaraju's algorithm to find strongly connected components in Python
from collections import defaultdict
......@@ -187,7 +187,7 @@ print("Strongly Connected Components:")
g.print_scc()
```
```
```java
// Kosaraju's algorithm to find strongly connected components in Java
import java.util.*;
......@@ -292,7 +292,7 @@ class Graph {
}
```
```
```cpp
// Kosaraju's algorithm to find strongly connected components in C++
#include <iostream>
......
......@@ -54,7 +54,7 @@
```
```py
# Adjacency Matrix representation in Python
class Graph(object):
......@@ -105,7 +105,7 @@ if __name__ == '__main__':
main()
```
```
```java
// Adjacency Matrix representation in Java
public class Graph {
......@@ -157,7 +157,7 @@ public class Graph {
}
```
```
```c
// Adjacency Matrix representation in C
#include <stdio.h>
......@@ -206,7 +206,7 @@ int main() {
}
```
```
```cpp
// Adjacency Matrix representation in C++
#include <iostream>
......
......@@ -103,9 +103,9 @@ graph = {'A': set(['B', 'C']),
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Adjascency List representation in Python
class AdjNode:
......@@ -151,7 +151,7 @@ if __name__ == "__main__":
graph.print_agraph()
```
```
```java
// Adjascency List representation in Java
import java.util.*;
......@@ -195,7 +195,7 @@ class Graph {
}
```
```
```c
// Adjascency List representation in C
#include <stdio.h>
......@@ -274,7 +274,7 @@ int main() {
}
```
```
```cpp
// Adjascency List representation in C++
#include <bits/stdc++.h>
......
......@@ -102,7 +102,7 @@ init() {
```
```py
# DFS algorithm in Python
# DFS algorithm
......@@ -126,7 +126,7 @@ graph = {'0': set(['1', '2']),
dfs(graph, '0')
```
```
```java
// DFS algorithm in Java
import java.util.*;
......@@ -177,7 +177,7 @@ class Graph {
}
```
```
```c
// DFS algorithm in C
#include <stdio.h>
......@@ -284,7 +284,7 @@ int main() {
}
```
```
```cpp
// DFS algorithm in C++
#include <iostream>
......
......@@ -98,7 +98,7 @@ while Q is non-empty
```
```py
# BFS algorithm in Python
import collections
......@@ -128,7 +128,7 @@ if __name__ == '__main__':
bfs(graph, 0)
```
```
```java
// BFS algorithm in Java
import java.util.*;
......@@ -192,7 +192,7 @@ public class Graph {
}
```
```
```c
// BFS algorithm in C
#include <stdio.h>
......@@ -364,7 +364,7 @@ int main() {
}
```
```
```cpp
// BFS algorithm in C++
#include <iostream>
......
......@@ -121,9 +121,9 @@ Dijkstra vs Bellman Ford 算法
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Bellman Ford Algorithm in Python
class Graph:
......@@ -177,7 +177,7 @@ g.add_edge(3, 2, 2)
g.bellman_ford(0)
```
```
```java
// Bellman Ford Algorithm in Java
class CreateGraph {
......@@ -287,7 +287,7 @@ class CreateGraph {
}
```
```
```c
// Bellman Ford Algorithm in C
#include <stdio.h>
......@@ -432,7 +432,7 @@ void display(int arr[], int size) {
}
```
```
```cpp
// Bellman Ford Algorithm in C++
#include <bits/stdc++.h>
......
......@@ -65,9 +65,9 @@ end bubbleSort
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Bubble sort in Python
def bubbleSort(array):
......@@ -89,7 +89,7 @@ print('Sorted Array in Asc ending Order:')
print(data)
```
```
```java
// Bubble sort in Java
import java.util.Arrays;
......@@ -124,7 +124,7 @@ class BubbleSort {
}
```
```
```c
// Bubble sort in C
#include <stdio.h>
......@@ -166,7 +166,7 @@ int main() {
}
```
```
```cpp
// Bubble sort in C++
#include <iostream>
......@@ -238,7 +238,7 @@ end bubbleSort
```
```py
# Optimized bubble sort in python
def bubbleSort(array):
......@@ -268,7 +268,7 @@ print('Sorted Array in Ascending Order:')
print(data)
```
```
```java
// Optimized bubble sort in Java
import java.util.Arrays;
......@@ -314,7 +314,7 @@ class BubbleSort {
}
```
```
```c
// Optimized bubble sort in C
#include <stdio.h>
......@@ -364,7 +364,7 @@ int main() {
}
```
```
```cpp
// Optimized bubble sort in C++
#include <iostream>
......@@ -372,9 +372,7 @@ using namespace std;
void bubbleSort(int array[], int size) {
for (int step = 0; step < size - 1; ++step) {
```
```
// Run loops two times: one for walking throught the array // and the other for comparison
int swapped = 0;
for (int i = 0; i < size - step - 1; ++i) {
......
......@@ -85,9 +85,9 @@ end selectionSort
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Selection sort in Python
def selectionSort(array, size):
......@@ -112,7 +112,7 @@ print('Sorted Array in Ascending Order:')
print(data)
```
```
```java
// Selection sort in Java
import java.util.Arrays;
......@@ -151,7 +151,7 @@ class SelectionSort {
}
```
```
```c
// Selection sort in C
#include <stdio.h>
......@@ -197,7 +197,7 @@ int main() {
}
```
```
```cpp
// Selection sort in C++
#include <iostream>
......
......@@ -79,9 +79,9 @@ end insertionSort
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Insertion sort in Python
def insertionSort(array):
......@@ -105,7 +105,7 @@ print('Sorted Array in Ascending Order:')
print(data)
```
```
```java
// Insertion sort in Java
import java.util.Arrays;
......@@ -143,7 +143,7 @@ class InsertionSort {
}
```
```
```c
// Insertion sort in C
#include <stdio.h>
......@@ -182,7 +182,7 @@ int main() {
}
```
```
```cpp
// Insertion sort in C++
#include <iostream>
......
......@@ -277,9 +277,9 @@ void merge(int arr[], int p, int q, int r) {
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# MergeSort in Python
def mergeSort(array):
......@@ -335,7 +335,7 @@ if __name__ == '__main__':
printList(array)
```
```
```java
// Merge sort in Java
class MergeSort {
......@@ -425,7 +425,7 @@ class MergeSort {
}
```
```
```c
// Merge sort in C
#include <stdio.h>
......@@ -512,7 +512,7 @@ int main() {
}
```
```
```cpp
// Merge sort in C++
#include <iostream>
......
......@@ -114,7 +114,7 @@ return storeIndex + 1
```
```py
# Quick sort in Python
# Function to partition the array on the basis of pivot element
......@@ -155,7 +155,7 @@ print('Sorted Array in Ascending Order:')
print(data)
```
```
```java
// Quick sort in Java
import java.util.Arrays;
......@@ -212,7 +212,7 @@ class QuickSort {
}
```
```
```c
// Quick sort in C
#include <stdio.h>
......@@ -277,7 +277,7 @@ int main() {
}
```
```
```cpp
// Quick sort in C++
#include <iostream>
......
......@@ -76,9 +76,9 @@ countingSort(array, size)
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Counting sort in Python programming
def countingSort(array):
......@@ -114,7 +114,7 @@ print("Sorted Array in Ascending Order: ")
print(data)
```
```
```java
// Counting sort in Java programming
import java.util.Arrays;
......@@ -171,7 +171,7 @@ class CountingSort {
}
```
```
```c
// Counting sort in C programming
#include <stdio.h>
......@@ -237,7 +237,7 @@ int main() {
}
```
```
```cpp
// Counting sort in C++ programming
#include <iostream>
......
......@@ -81,9 +81,9 @@ countingSort(array, d)
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Radix sort in Python
# Using counting sort to sort the elements in the basis of significant places
......@@ -128,7 +128,7 @@ radixSort(data)
print(data)
```
```
```java
// Radix Sort in Java Programming
import java.util.Arrays;
......@@ -197,7 +197,7 @@ class RadixSort {
}
```
```
```c
// Radix Sort in C Programming
#include <stdio.h>
......@@ -270,7 +270,7 @@ int main() {
}
```
```
```cpp
// Radix Sort in C++ Programming
#include <iostream>
......
......@@ -99,9 +99,9 @@ end bucketSort
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Bucket Sort in Python
def bucketSort(array):
......@@ -133,7 +133,7 @@ print("Sorted Array in descending order is")
print(bucketSort(array))
```
```
```java
// Bucket sort in Java
import java.util.ArrayList;
......@@ -183,7 +183,7 @@ public class BucketSort {
}
```
```
```c
// Bucket sort in C
#include <stdio.h>
......@@ -340,7 +340,7 @@ int main(void) {
}
```
```
```cpp
// Bucket sort in C++
#include <iomanip>
......
......@@ -235,9 +235,9 @@ void heapify(int arr[], int n, int i) {
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Heap Sort in python
def heapify(arr, n, i):
......@@ -279,7 +279,7 @@ void heapify(int arr[], int n, int i) {
print("%d " % arr[i], end='')
```
```
```java
// Heap Sort in Java
public class HeapSort {
......@@ -346,7 +346,7 @@ void heapify(int arr[], int n, int i) {
}
```
```
```c
// Heap Sort in C
#include <stdio.h>
......@@ -411,7 +411,7 @@ void heapify(int arr[], int n, int i) {
}
```
```
```cpp
// Heap Sort in C++
#include <iostream>
......
......@@ -94,9 +94,9 @@ end shellSort
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Shell sort in python
def shellSort(array, n):
......@@ -121,7 +121,7 @@ print('Sorted Array in Ascending Order:')
print(data)
```
```
```java
// Shell sort in Java programming
import java.util.Arrays;
......@@ -155,7 +155,7 @@ class ShellSort {
}
```
```
```c
// Shell Sort in C programming
#include <stdio.h>
......@@ -193,7 +193,7 @@ int main() {
}
```
```
```cpp
// Shell Sort in C++ programming
#include <iostream>
......
......@@ -51,9 +51,9 @@ LinearSearch(array, key)
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Linear Search in Python
def linearSearch(array, n, x):
......@@ -74,7 +74,7 @@ else:
print("Element found at index: ", result)
```
```
```java
// Linear Search in Java
class LinearSearch {
......@@ -103,7 +103,7 @@ class LinearSearch {
}
```
```
```c
// Linear Search in C
#include <stdio.h>
......@@ -128,7 +128,7 @@ int main() {
}
```
```
```cpp
// Linear Search in C++
#include <iostream>
......
......@@ -108,9 +108,9 @@ binarySearch(arr, x, low, high)
## Python,Java,C/C++ 示例(迭代方法)
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Binary Search in python
def binarySearch(array, x, low, high):
......@@ -142,7 +142,7 @@ else:
print("Not found")
```
```
```java
// Binary Search in Java
class BinarySearch {
......@@ -179,7 +179,7 @@ class BinarySearch {
}
```
```
```c
// Binary Search in C
#include <stdio.h>
......@@ -215,7 +215,7 @@ int main(void) {
}
```
```
```cpp
// Binary Search in C++
#include <iostream>
......@@ -256,9 +256,9 @@ int main(void) {
## Python,Java,C/C++ 示例(递归方法)
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Binary Search in python
def binarySearch(array, x, low, high):
......@@ -293,7 +293,7 @@ else:
print("Not found")
```
```
```java
// Binary Search in Java
class BinarySearch {
......@@ -331,7 +331,7 @@ class BinarySearch {
}
```
```
```c
// Binary Search in C
#include <stdio.h>
......@@ -367,7 +367,7 @@ int main(void) {
}
```
```
```cpp
// Binary Search in C++
#include <iostream>
......
......@@ -115,9 +115,9 @@ Ford-Fulkerson 算法是一种[贪婪方法](http://programiz.com/dsa/greedy-alg
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Ford-Fulkerson algorith in Python
from collections import defaultdict
......@@ -190,7 +190,7 @@ sink = 5
print("Max Flow: %d " % g.ford_fulkerson(source, sink))
```
```
```java
// Ford-Fulkerson algorith in Java
import java.util.LinkedList;
......@@ -269,7 +269,7 @@ class FordFulkerson {
}
```
```
```c
/ Ford - Fulkerson algorith in C
#include <stdio.h>
......@@ -377,7 +377,7 @@ int main() {
}
```
```
```cpp
// Ford-Fulkerson algorith in C++
#include <limits.h>
......
......@@ -110,9 +110,9 @@ function dijkstra(G, S)
下面给出了 Dijkstra 算法在 C++ 中的实现。 可以提高[代码](http://www.reviewmylife.co.uk/blog/2008/07/15/dijkstras-algorithm-code-in-c/)的复杂度,但是抽象方法很容易将代码与算法相关联。
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Dijkstra's Algorithm in Python
import sys
......@@ -176,7 +176,7 @@ for distance in visited_and_distance:
i = i + 1
```
```
```java
// Dijkstra's Algorithm in Java
public class Dijkstra {
......@@ -233,7 +233,7 @@ public class Dijkstra {
}
```
```
```c
// Dijkstra's Algorithm in C
#include <stdio.h>
......@@ -356,7 +356,7 @@ int main() {
}
```
```
```cpp
// Dijkstra's Algorithm in C++
#include <iostream>
......
......@@ -87,9 +87,9 @@ return A
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Kruskal's algorithm in Python
class Graph:
......@@ -159,7 +159,7 @@ g.add_edge(5, 4, 3)
g.kruskal_algo()
```
```
```java
// Kruskal's algorithm in Java
import java.util.*;
......@@ -284,7 +284,7 @@ class Graph {
}
```
```
```c
// Kruskal's algorithm in C
#include <stdio.h>
......@@ -441,7 +441,7 @@ int main() {
}
```
```
```cpp
// Kruskal's algorithm in C++
#include <algorithm>
......
......@@ -84,9 +84,9 @@ while (U ≠ V)
尽管使用图形的[邻接矩阵](/dsa/graph-adjacency-matrix)表示,但是也可以使用[邻接表](/dsa/graph-adjacency-list)来实现此算法,以提高效率。
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Prim's Algorithm in Python
INF = 9999999
......@@ -133,7 +133,7 @@ while (no_edge < V - 1):
no_edge += 1
```
```
```java
// Prim's Algorithm in Java
import java.util.Arrays;
......@@ -212,7 +212,7 @@ class PGraph {
}
```
```
```c
// Prim's Algorithm in C
#include<stdio.h>
......@@ -290,7 +290,7 @@ int main() {
}
```
```
```cpp
// Prim's Algorithm in C++
#include <cstring>
......
......@@ -130,9 +130,9 @@ return rootNode
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Huffman Coding in python
string = 'BCAADDDCCACACAC'
......@@ -192,7 +192,7 @@ for (char, frequency) in freq:
print(' %-4r |%12s' % (char, huffmanCode[char]))
```
```
```java
// Huffman Coding in Java
import java.util.PriorityQueue;
......@@ -272,7 +272,7 @@ public class Huffman {
}
```
```
```c
// Huffman Coding in C
#include <stdio.h>
......@@ -456,7 +456,7 @@ int main() {
}
```
```
```cpp
// Huffman Coding in C++
#include <iostream>
......
......@@ -97,9 +97,9 @@ return A
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Floyd Warshall Algorithm in python
# The number of vertices
......@@ -135,7 +135,7 @@ G = [[0, 3, INF, 5],
floyd_warshall(G)
```
```
```java
// Floyd Warshall Algorithm in Java
class FloydWarshall {
......@@ -182,7 +182,7 @@ class FloydWarshall {
}
```
```
```c
// Floyd-Warshall Algorithm in C
#include <stdio.h>
......@@ -235,7 +235,7 @@ int main() {
}
```
```
```cpp
// Floyd-Warshall Algorithm in C++
#include <iostream>
......
......@@ -142,9 +142,9 @@ Compare X[i] and Y[j]
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# The longest common subsequence in Python
# Function to find lcs_algo
......@@ -192,7 +192,7 @@ n = len(S2)
lcs_algo(S1, S2, m, n)
```
```
```java
// The longest common subsequence in Java
class LCS_ALGO {
......@@ -250,7 +250,7 @@ class LCS_ALGO {
}
```
```
```c
// The longest common subsequence in C
#include <stdio.h>
......@@ -311,7 +311,7 @@ int main() {
}
```
```
```cpp
// The longest common subsequence in C++
#include <iostream>
......
......@@ -124,9 +124,9 @@ for s = 0 to n - m
## Python,Java 和 C/C++ 示例
[Python](#python-code)[Java](#java-code)[C](#c-code)[C++](#cpp-code)
```
```py
# Rabin-Karp algorithm in python
d = 10
......@@ -171,7 +171,7 @@ q = 13
search(pattern, text, q)
```
```
```java
// Rabin-Karp algorithm in Java
public class RabinKarp {
......@@ -223,7 +223,7 @@ public class RabinKarp {
}
```
```
```c
// Rabin-Karp algorithm in C
#include <stdio.h>
......@@ -277,7 +277,7 @@ int main() {
}
```
```
```cpp
// Rabin-Karp algorithm in C++
#include <string.h>
......
......@@ -74,7 +74,7 @@
```
```py
# Stack implementation in python
# Creating a stack
......@@ -107,7 +107,7 @@ print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))
```
```
```java
// Stack implementation in Java
class Stack {
......@@ -180,7 +180,7 @@ class Stack {
}
```
```
```c
// Stack implementation in C
#include <stdio.h>
......@@ -270,7 +270,7 @@ int main() {
}
```
```
```cpp
// Stack implementation in C++
#include <stdlib.h>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册