Division and Recursion-QuickSort

#include<iostream>
using namespace std;

void swap(int array[], int index1, int index2){
    int t = array[index1];
    array[index1] = array[index2];
    array[index2] = t;
    return;
}

int partition(int array[], int beginPos, int endPos)
{
    int comparedVal = array[beginPos];
    int cursorFromBegin = beginPos + 1;
    int cursorFromEnd = endPos;

while(true){
         while (array[cursorFromBegin] < comparedVal){cursorFromBegin++;}
         while (array[cursorFromEnd] > comparedVal){cursorFromEnd--;}
         if (cursorFromBegin>=cursorFromEnd){break;}
         swap(array, cursorFromBegin, cursorFromEnd);
     }
     swap(array,cursorFromEnd,beginPos);
     return cursorFromEnd;
}

void quickSort(int array[],int begin,int end){
    if (begin  >= end){cout<<begin<<end<<endl;return;}
    int k =  partition(array,begin,end);
    quickSort(array, begin,k-1);
    quickSort(array, k+1, end);
}

int main(){
    int array[6]={3,5,9,8,7,5};
    quickSort(array, 0, 5);
    for(int i =0;i<6;i++){cout<<array[i]<<endl;}

}

时间: 2024-10-22 06:47:16

Division and Recursion-QuickSort的相关文章

Algorithm | Sort

Bubble sort Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wron

Balanced and stabilized quicksort method

The improved?Quicksort?method of the present invention utilizes two pointers initialized at opposite ends of the array or partition to be sorted and an initial partition value Pvalue located at the center of the array or partition. The value at each

Binary Search and Quicksort

1. stdlib/bsearch.c 以下代码实现了有序数组的二分搜索. 1 /* $NetBSD: bsearch.c,v 1.14.6.1 2012/04/17 00:05:25 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in sou

递归和回溯(recursion and backtracking)

1. Concept Backtracking is a refinement of the brute force approach, which systematically searches for a solution to a problem among all available options. It does so by assuming that the solutions are represented by vectors (v1, ..., vm) of values a

light oj 1214 - Large Division

1214 - Large Division   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if

fzuoj1607Greedy division

题目链接: 点我点我 题目:  Problem 1607 Greedy division Accept: 436    Submit: 1590 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunate

Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”

今天在写爬虫的时候,发现了一个诡异的事情,使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是"maximum recursion depth exceeded while calling a Python object",意思大致是"当调用该对象超过最大递归深度" 报错如下:   Traceback (most recent call last):   File "<stdin>", line 1, 

HDU 3480 Division(斜率优化+二维DP)

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 3984    Accepted Submission(s): 1527 Problem Description Little D is really interested in the theorem of sets recently. There’s a pro

数组去重算法,quickSort

function removeRepeat(arr) { var arr2 = [] ,obj = {}; for (var i = 0; i<arr.length; i++) { var num = arr[i]; //先把arr的第[i]num if( !obj[num] ){ //如果上面有个true,那么就不要push进数组,否则就push进数组 arr2.push(num); obj[num] = true; //不要忘记push到数组以后把obj上的属性设置为true,那么下次有一样