Recursive version of the insertion sort
2007-02-04 20:49
337 查看
// RecursiveInsertionSort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void PrintArray(int a[], int iStart, int iEnd);
void InsertionSort(int* a, int length);
int _tmain(int argc, _TCHAR* argv[])
{
// sorted elements start from 1
int arrayToSort[] = {INT_MIN, 20, 7, 3, 4, 25, 15, 29, 12, 4, 1};
int n = sizeof(arrayToSort)/sizeof(int);
PrintArray(arrayToSort, 1, n-1);
InsertionSort(arrayToSort, n-1);
PrintArray(arrayToSort, 1, n-1);
return 0;
}
void InsertionSort(int* a, int length)
{
if(length>1)
{
// sort the element before last element
InsertionSort(a, length-1);
// insert the last element into the sorted sequence
int j = length - 1;
int key = a[length];
while(j>0 && a[j]>key)
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
}
void PrintArray(int a[], int iStart, int iEnd)
{
for(int i=iStart; i<= iEnd; i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void PrintArray(int a[], int iStart, int iEnd);
void InsertionSort(int* a, int length);
int _tmain(int argc, _TCHAR* argv[])
{
// sorted elements start from 1
int arrayToSort[] = {INT_MIN, 20, 7, 3, 4, 25, 15, 29, 12, 4, 1};
int n = sizeof(arrayToSort)/sizeof(int);
PrintArray(arrayToSort, 1, n-1);
InsertionSort(arrayToSort, n-1);
PrintArray(arrayToSort, 1, n-1);
return 0;
}
void InsertionSort(int* a, int length)
{
if(length>1)
{
// sort the element before last element
InsertionSort(a, length-1);
// insert the last element into the sorted sequence
int j = length - 1;
int key = a[length];
while(j>0 && a[j]>key)
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
}
void PrintArray(int a[], int iStart, int iEnd)
{
for(int i=iStart; i<= iEnd; i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}
相关文章推荐
- 笔试算法题(56):快速排序实现之非递归实现,最小k值选择(non-recursive version, Minimal Kth Selection of Quick Sort)
- 算法导论 习题2.3-4(the implemention of insertion sort recursively)
- 【ask】Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
- 解决java compiler level does not match the version of the installed java project facet的办法
- You are attempting to build with the incorrect version of java
- 解决java compiler level does not match the version of the installed java project facet
- java compiler level does not match the version of the installed java project facet
- eclipse中切换jre后报错:Java compiler level does not match the version of the installed Java project facet.
- Maven构建项目后项目报Error错误Java compiler level does not match the version of the installed Java project fac
- It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing ___Error Installing APK
- Version 1.6.0 of the JVM is not suitable for the this product.Version:1.8 or greater is required
- 解决java compiler level does not match the version of the installed java project facet
- version 1.5.2-04 of the jvm is not suitable for this product. version:1.6 or greater is required
- Eclipse错误:Version 1.5.0_01 of the JVM is not suitable for this product
- New Version of the VS 2010 Productivity Power Tools is Available
- 转 Android SDK更新后提示This version of ADT requires the...your SDK to the latest version
- Failed to parse the output of 'adb version'
- 解决java compiler level does not match the version of the installed java project facet
- The serializable class XXX does not declare a static final serialVersionUID field of type long
- Java compiler level does not match the version of the installed Java project fac