Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Python 1-5 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
hustcc merged 13 commits into hustcc:master from LokiSharp:patch-1
Jan 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion 1.bubbleSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ function bubbleSort(arr) {
}
return arr;
}
```
```


## 5. Python 代码实现

```python
def bubbleSort(arr):
for i in range(1, len(arr)):
for j in range(0, len(arr)-i):
if arr[j] > arr[j+1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
```
13 changes: 12 additions & 1 deletion 2.selectionSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ function selectionSort(arr) {
}
return arr;
}
```
```

## 4. Python 代码实现

```python
def selectionSort(arr):
for i in range(len(arr)-1):
for j in range(i+1, len(arr)):
if arr[j] < arr[i]:
arr[i], arr[j] = arr[j], arr[i]
return arr
```
16 changes: 15 additions & 1 deletion 3.insertionSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@ function insertionSort(arr) {
}
return arr;
}
```
```

## 4. Python 代码实现

```python
def insertionSort(arr):
for i in range(len(arr)):
preIndex = i-1
current = arr[i]
while preIndex >= 0 and arr[preIndex] > current:
arr[preIndex+1] = arr[preIndex]
preIndex-=1
arr[preIndex+1] = current
return arr
'''
23 changes: 22 additions & 1 deletion 4.shellSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@ function shellSort(arr) {
}
return arr;
}
```
```

## 3. Python 代码实现

```python
def shellSort(arr):
import math
gap=1
while(gap < len(arr)/3):
gap = gap*3+1
while gap > 0:
for i in range(gap,len(arr)):
temp = arr[i]
j = i-gap
while j >=0 and arr[j] > temp:
arr[j+gap]=arr[j]
j-=gap
arr[j+gap] = temp
gap = math.floor(gap/3)
return arr
}
```
27 changes: 26 additions & 1 deletion 5.mergeSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,29 @@ function merge(left, right)

return result;
}
```
```

## 5. Python 代码实现

```python
def mergeSort(arr):
import math
if(len(arr)<2):
return arr
middle = math.floor(len(arr)/2)
left, right = arr[0:middle], arr[middle:]
return merge(mergeSort(left), mergeSort(right))

def merge(left,right):
result = []
while left and right:
if left[0] <= right[0]:
result.append(left.pop(0));
else:
result.append(right.pop(0));
while left:
result.append(left.pop(0));
while right:
result.append(right.pop(0));
return result
```
31 changes: 30 additions & 1 deletion 6.quickSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,33 @@ function swap(arr, i, j) {
arr[i] = arr[j];
arr[j] = temp;
}
```
```


## 4. Python 代码实现

```python
def quickSort(arr, left=None, right=None):
left = 0 if not isinstance(left,(int, float)) else left
right = len(arr)-1 if not isinstance(right,(int, float)) else right
if left < right:
partitionIndex = partition(arr, left, right)
quickSort(arr, left, partitionIndex-1)
quickSort(arr, partitionIndex+1, right)
return arr

def partition(arr, left, right):
pivot = left
index = pivot+1
i = index
while i <= right:
if arr[i] < arr[pivot]:
swap(arr, i, index)
index+=1
i+=1
swap(arr,pivot,index-1)
return index-1

def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]
```
36 changes: 35 additions & 1 deletion 7.heapSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,38 @@ function heapSort(arr) {
}
return arr;
}
```
```
## 4. Python 代码实现

```python
def buildMaxHeap(arr):
import math
for i in range(math.floor(len(arr)/2),-1,-1):
heapify(arr,i)

def heapify(arr, i):
left = 2*i+1
right = 2*i+2
largest = i
if left < arrLen and arr[left] > arr[largest]:
largest = left
if right < arrLen and arr[right] > arr[largest]:
largest = right

if largest != i:
swap(arr, i, largest)
heapify(arr, largest)

def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]

def heapSort(arr):
global arrLen
arrLen = len(arr)
buildMaxHeap(arr)
for i in range(len(arr)-1,0,-1):
swap(arr,0,i)
arrLen -=1
heapify(arr, 0)
return arr
```
21 changes: 20 additions & 1 deletion 8.countingSort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ function countingSort(arr, maxValue) {

return arr;
}
```
```
## 3. JavaScript 代码实现

```python
def countingSort(arr, maxValue):
bucketLen = maxValue+1
bucket = [0]*bucketLen
sortedIndex =0
arrLen = len(arr)
for i in range(arrLen):
if not bucket[arr[i]]:
bucket[arr[i]]=0
bucket[arr[i]]+=1
for j in range(bucketLen):
while bucket[j]>0:
arr[sortedIndex] = j
sortedIndex+=1
bucket[j]-=1
return arr
```

AltStyle によって変換されたページ (->オリジナル) /