(转载)
进入vi的命令
进入vi的命令
Vi filename 打开或新建文件,并将光标置于第一行首
Vi +n filename 打开文件,并将光标置于第n行首
Vi + filename 打开文件并将光标置于最后一行首
Vi +/pattern filename 打开文件,并将光标置于一个与pattern匹配的串处
Vi filename .. Filename 打开多个文件,依次进行编辑
//compare the consecutive numbers, larger one sinks to the bottom void bubbleSort(int *a) { // int temp; for(int j = 0; j < arraysize; j++){
for( int i = 0; i < arraysize - j - 1;i++ )
{
if( a[i] > a[i+1] ) {swap(a[i], a[i+1]);} } } }
Heapsort
Posted at
6/19/2009 03:34:00 PM
“Like merge sort, but unlike insertion sort, heapsort’s running time is O(n*lg(n)). Like insertion sort, but unlike merge sort, heapsort sorts in place: only a constant number of array elements are stored outside the input array at any time. Thus, heapsort combines the better attribute of the two sorting algorithms …” [Introduction to Algorithms]
void heapSort(int *a)
{
for(int i = arraySize; i > 1; --i)
{
for(int k = 0; k < i; ++k)
{
// compare each element with its parent
// if the child is begger than its parent
// then swap them
// keep doing this until reaching the root
while((a[k] > a[k/2]) && (k > 0))
{
swap(a[k], a[k/2]);
k = k/2;
}
}
// After the for loop, the largest is put at a[0]
// then put the largest to the end of the array
// and find the largest of the rest in the same way
swap(a[0], a[i-1]);
}
}
Insertion Sort
Posted at
6/14/2009 06:47:00 PM
Insertion sort has a worst-case running time of Θ(n^2).
void insertionSort(int *a, int n)
{
for (int j = 1; j < n; ++j)
{
int key = a[j];
// Insert A[j] into the sorted sequence A[0, 1,...,j-1]
int i = j-1;
while ((i >= 0) && (key < a[i]))
{
a[i+1] = a[i];
--i;
}
a[i+1] = key;
}
}
Subscribe to:
Posts (Atom)