�����㷨
Ϊʲô����ô���������㷨?
���ȣ��ڼ�����������������һ���������������⡣����ֻ�о��������Ÿ������塣���Σ������㷨˵����������Ҫ���㷨�ļ���������������ϸ�֣��ݹ����������ӡ�����Ҫ˵����һ���Dz�ͬ���㷨�в�ͬ����ȱ�㣬û��һ���㷨���κ������¶������õ��㷨��
����������
���㷨��ר�������Ѳ������������ݽ���������һ�������㷨�����������������嵥��ֻ��һ���������������Ļ����������㷨���������������㷨���������������嵥�е��������������еģ���ô���ַ����ͳ����������㷨�ˡ�������ʹ�������㷨֮ǰһ��Ҫ���ء�
�����㷨�ĺ���˼����ɨ�������嵥��Ѱ�ҳ����������������ڵ���Ŀ�����ҵ���������Ŀ������Ŀ��λ��Ȼ������ɨ�衣�ظ������IJ���ֱ�����е���Ŀ����˳���źá�
ͼ1�Ƕ������㷨��˵�����ڸ����У�����1��δ��˳���źá���һ��ɨ���嵥ʱ�������ҵ�4��1���������ڵ�������Ŀ�����ǽ������ǵ�λ�á��Դ����ƣ�ֱ�������е���Ŀ��1234�źá�����1��������������һ������������һ�㷨���Ƶ�������
| 2 | 2 | 2 | 1 |
| 3 | 3 | 1 | 2 |
| 4 | 1 | 3 | 3 |
| 1 | 4 | 4 | 4 |
�����ԸĽ����㷨���ó������¶��Ͽ�ʼɨ�裬����ֻ��һ�ξ����ź�˳���ˡ�
��������VB����ʵ����һ�㷨�����ӣ�
' min and max are the minimum and maximum indexes ' of the items that might still be out of order. Sub BubbleSort (List() As Long, ByVal min As Integer, _ ByVal max As Integer) Dim last_swap As Integer Dim i As Integer Dim j As Integer Dim tmp As Long ' Repeat until we are done. Do While min < max ' Bubble up. last_swap = min - 1 ' For i = min + 1 To max i = min + 1 Do While i <= max ' Find a bubble. If List(i - 1) > List(i) Then ' See where to drop the bubble. tmp = List(i - 1) j = i Do List(j - 1) = List(j) j = j + 1 If j > max Then Exit Do Loop While List(j) < tmp List(j - 1) = tmp last_swap = j - 1 i = j + 1 Else i = i + 1 End If Loop ' Update max. max = last_swap - 1 ' Bubble down. last_swap = max + 1 ' For i = max - 1 To min Step -1 i = max - 1 Do While i >= min ' Find a bubble. If List(i + 1) < List(i) Then ' See where to drop the bubble. tmp = List(i + 1) j = i Do List(j + 1) = List(j) j = j - 1 If j < min Then Exit Do Loop While List(j) > tmp List(j + 1) = tmp last_swap = j + 1 i = j - 1 Else i = i - 1 End If Loop ' Update min. min = last_swap + 1 Loop End Sub |
ѡ�������� ѡ����������һ���ܼ��㷨����ԭ���������ҵ������嵥�е���С�����ݣ�Ȼ������������ͬ��һ�����ݽ���λ�ã��������ҵڶ�С�����ݣ��ٽ���ͬ�ڶ������ݽ���λ�ã��Դ����ơ�������VB����ʵ�ָ��㷨��
Sub Selectionsort (List() As Long, min As Integer, _
max As Integer)
Dim i As Integer
Dim j As Integer
Dim best_value As Long
Dim best_j As Integer
For i = min To max - 1
best_value = List(i)
best_j = i
For j = i + 1 To max
If List(j) < best_value Then
best_value = List(j)
best_j = j
End If
Next j
List(best_j) = List(i)
List(i) = best_value
Next i
End Sub |
��Ѱ�ҵ�IС������ʱ������������N-I����Ŀ��������һ�㷨���õIJ�����������������ʽ���á�
N + (N - 1) + (N - 2) + ... + 1 = N * (N + 1) / 2
ѡ�������������ڽ������ݵ������������㷨�ϼ��������Ĵ���Ҳ�Ƚϼ�����Ϊ����ά�����������˷��㡣ʵ�����������������൱�ٵĻ��������㷨���ٶȿ������������ӵ��㷨��
����������
���������ڴ������ݵ������ر����á�������ԭ���ǣ����ȼ��������б��е�������������С����������ֱ���˳������������г����������ϵ����ݣ���ѡ��һ���ָ��㽫���ݷֳ��������֣�С�ڷָ��������ݷ���һ�飬�����ķ�����һ�飬Ȼ���ֱ�����������������
ͨ���ָ���������������ѡȡ�ġ������������������Ƿ��ѱ����й��������ָ��ɵ��������б��Ĵ�С�Dz���ġ���ֻҪ�������б��Ĵ�С��࣬���㷨�����IJ�������N * log(N) ��������ʹ�ñȽϷ������������㷨�������������ķ�������������VB����ʵ����һ�㷨�����ӡ�
Sub Quicksort (List() As Long, min As Integer, max As Integer)
Dim med_value As Long
Dim hi As Integer
Dim lo As Integer
Dim i As Integer
' If the list has no more than 1 element, it's sorted.
If min >= max Then Exit Sub
' Pick a dividing item.
i = Int((max - min + 1) * Rnd + min)
med_value = List(i)
' Swap it to the front so we can find it easily.
List(i) = List(min)
' Move the items smaller than this into the left
' half of the list. Move the others into the right.
lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While List(hi) >= med_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
List(lo) = med_value
Exit Do
End If
' Swap the lo and hi values.
List(lo) = List(hi)
' Look up from lo for a value >= med_value.
lo = lo + 1
Do While List(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
List(hi) = med_value
Exit Do
End If
' Swap the lo and hi values.
List(hi) = List(lo)
Loop
' Sort the two sublists
Quicksort List(), min, lo - 1
Quicksort List(), lo + 1, max
End Sub |
����������
ǰ���ᵽ��������ʹ�ñȽϷ���Ϊ�㷨�������㷨��˵��������N * log(N)�����������������������ñȽϣ����������ܴ����ơ�ʵ���ϸ��㷨������֮�죬������������Ϊ��ʹ����ħ������������ѧ��������������һ���棬����������ֻ���������������������ȣ����е�Ҫ�������������ݱ��������������ܶ��ַ�ʹ�ø��㷨�����Σ����ݵķ�Χ���ޣ�����������������1��1000֮�ڣ��������㷨��Ч���ͷdz��ã�������������������1��30000֮�䣬���㷨���������á�
���ȸ��㷨����һ���������͵���ʱ���飬�����������±��ֱ���Ҫ���������ݵ�������Сֵ�����������б���������Сֵ��min_item��max_item, ���㷨�ͽ����鴴�������������� Dim Counts() As Integer
ReDim Counts(min_item To max_item)���������㷨�����б��е�ÿһ����Ŀ�������Ӷ�Ӧ����Ŀ������Ԫ�ص�ֵ������һ�����ɺ���Counts(I)����ֵ���Ǿ�����ֵΪI�Ļ����ϵĸ����� For I = min To Max
Counts(List(I)) = Counts(List(I)) + 1
Next I����ɨ��Counts���飬��countsת���ɱ������б��е�ƫ������ next_spot = 1
For i = min_value To max_value
this_count = counts(i)
counts(i) = next_spot
next_spot = next_spot + this_count
Next i���������ݽ�������
������ʵ�ָ��㷨��VB����
Sub Countingsort (List() As Long, sorted_list() As Long, _
min As Integer, max As Integer, min_value As Long, _
max_value As Long)
Dim counts() As Integer
Dim i As Integer
Dim this_count As Integer
Dim next_offset As Integer
' Create the Counts array.
ReDim counts(min_value To max_value)
' Count the items.
For i = min To max
counts(List(i)) = counts(List(i)) + 1
Next i
' Convert the counts into offsets.
next_offset = min
For i = min_value To max_value
this_count = counts(i)
counts(i) = next_offset
next_offset = next_offset + this_count
Next i
' Place the items in the sorted array.
For i = min To max
sorted_list(counts(List(i))) = List(i)
counts(List(i)) = counts(List(i)) + 1
Next i
End Sub |
�ܽ�
�±��Ǹ����㷨����ȱ���Ƚϣ���������������������ÿ���㷨ֻ����ijһ�����±��ֵ����ã�������ѡ���㷨ʱ��һЩԭ����
- �������������б�����99%�������Ź�����������������
- ������Ҫ���������ݽ���(100������)������ѡ������
- �����������ݶ���������������ֵ����(��ǧ����)�����ü�������
- �����Ļ��ÿ�������
| �㷨 | �ŵ� | ȱ�� |
|---|---|---|
| ���������� | ���Գ���������������˵���ַ������ٶȺܿ� | �����������������ٶȽ��� |
| ѡ�������� | �dz����� | �Դ������ݵ������ٶȺ��� |
| �������� | ||
| �����������ݵ�������˵�ٶȺܿ� | ||
| ���������� | �Դ������ݵ�������˵�ٶȺܿ� | �����д����ظ������ݾͱȽ��鷳 |
| ���������� | ��������ֵ��С(1-1000֮��)ʱ���ٶȷdz��� | ��������ֵ�ϴ�ʱ���ٶȽ��� |
| ���������ڴ� | ||
| ֻ�ܶ��������͵��������� |
- ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
- �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
- �������е���ɫ
- �������ж�IP��ַ��A��B���C��
- ������ϵ��Ц�� (ʷ����ȫ��
- ��Google���������ŵ���ͨv4.0
- ���ٶ�ͳ��URL��hm������utm�����Ķ�Ӧ��ϵ
- ���������ԡ���װ Windows XP ����ϵͳ��ͼ��ȫ����
- ��ƻ������MACbook Air���ݼ���ȫ
- ��Linux��ʹ��host��dig��nslookup��ѯDNS
- �����Լ���VPS�Google��������
- ��С�ڴ�VPS֮Nginx+PHP-fpm�߸����Ż���ѹ�����Է���
- ������������������̸�����ɶ��ر�
- ��������������ѡʳƷ����2020�ļ��ȵ�Ͷ�ʲ��Ժ���������
- �����ڵ�����֤50���ƴ�50����ָ֤�������Ĺ���
- ����֤50����Щ��Ʊ_��֤50Ȩ����������
- ������300����֤50����֤500��ָ��2019���ڶ��ζ��ڵ���������
- ������300��ֵ/�ɳ�ָ�����Ʒ����������й�ƽ��Ȩ���½�
- ����ָ֤��������֤50��ָ��������
- ����ָ֤��������֤����ָ��������
- ����ָ֤��������֤100ָ��������
- ����ָ֤����������300ָ��������