Division and Recursion-find middle element

#include <iostream>
using namespace std;

void swap(int array[], int beginPos, int endPos){
    int t = array[beginPos];
    array[beginPos] = array[endPos];
    array[endPos]=t;
}

int partition(int array[], int begin, int end, int val)
{
    int headCursor = begin;
    int tailCursor = end;

while(true)
    {
        while(array[headCursor] < val && headCursor < end) headCursor++;
        while(array[tailCursor] > val) tailCursor--;
        if (headCursor >= tailCursor){break;}
        swap(array, headCursor, tailCursor);
    }

return tailCursor;
}

int partition2(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 =  partition2(array,begin,end);
    quickSort(array, begin,k-1);
    quickSort(array, k+1, end);
}

int select_middle_element(int array[], int begin,int end, int k){
    if (end - begin <=10){
        quickSort(array, begin,end);
        return array[(end+begin)/2];
    }

int groupNum = (end-begin-4)/5;
    int groupBegin = begin;
    int groupEnd = begin + 4;
    int arraybegin = begin;
   
    for (int i = 0; i <groupNum;i++){
        quickSort(array, groupBegin, groupEnd);
        swap(array, arraybegin, (groupBegin+groupEnd)/2);
        groupBegin = groupEnd + 1;
        groupEnd = groupBegin +4;
        arraybegin++;
    }
   
    int middleVal = select_middle_element(array, begin, begin+(groupNum-1), (groupNum/2));

cout<<"middle "<<middleVal<<endl;
    int pos = partition(array, begin, end, middleVal);
    int j = pos - begin;

cout<<"pos "<<pos<<endl;
    cout<<"k "<<k<<endl;
    cout<<"begin"<<begin<<endl;

for (int i = 0;i<=24;i++)
     {
         cout<<array[i]<<" ";
     }
     cout<<endl;

for (int i = begin;i<=end;i++)
     {
         cout<<array[i]<<" ";
     }
     cout<<endl;
    if (k > j){
        return select_middle_element(array, pos, end, k-j);
    }else if (k==j){return array[k+begin];}
    else{
        return select_middle_element(array, begin, pos, k);
    }

return k;
}

int main(){
    int array[25]={4,7,1,2,3,
                   9,8,10,13,56,
                   90,100,877,900,1000,
                   30,89,40,45,28,
                   85};
   
    cout<<"middle value"<<select_middle_element(array, 0, 20, 10)<<endl;
    quickSort(array, 0, 20);

/*1 if n <=75, get the middle element .*/
    /*1 divide the n elements into n-4/5 gorups, find the middle element of each group
    (every group only have 5 elements), and find the middle element among the middle elements
    of each group, and then kick out those elements that are not possible*/
    for (int i = 0;i<=20;i++)
    {
        cout<<i<<":"<<array[i]<<endl;
    }
}

时间: 2024-10-09 02:06:24

Division and Recursion-find middle element的相关文章

Majority Element 解答

Solution 1 Naive way First, sort the array using Arrays.sort in Java. Than, scan once to find the majority element. Time complexity O(nlog(n)) 1 public class Solution { 2 public int majorityElement(int[] nums) { 3 int length = nums.length; 4 if (leng

problem report: middle of linked list

Middle of Linked List propose: get the middle element of a linked list method: 1. use two pointers conplexity: o(n) example: Given 1->2->3, return the node with value 2. Given 1->2, return the node with value 1. mycode: ListNode firstPointer = ne

Searching an Element in a Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). How do you find an element in the rotated array efficiently? You may assume no duplicate exists in the array. Note:I have upd

分治法求连续子数组的最大和

思路来自算法导论,将数组平分为左右两个子数组,那么最大子数组可能在左边的子数组中,也有可能在右边的子数组中,还有可能跨过中间的元素,只有这三种情况.对于前两种情况,可以使用递归求解,对于第三种情况,可以做到用线性时间复杂度的函数来求解,详见代码. #include <iostream> #include <map> using namespace std; //the thought of this algrithm is devide-and-couque int int_max

LeetCode – Refresh – Convert Sorted Array to Binary Search Tree

It is kind of binary search. Since this is a sorted array, we can treat it as inorder traversal. And we can define the root node for the Tree. So find the middle element as the root, then keep doing recursion. 1 /** 2 * Definition for binary tree 3 *

Oracle 10gR2分析函数

Oracle 10gR2分析函数汇总 (Translated By caizhuoyi 2008‐9‐19) 说明:  1. 原文中底色为黄的部分翻译存在商榷之处,请大家踊跃提意见:  2. 原文中淡蓝色字体的文字,不宜翻译,保持原样.  1. ANALYTIC FUNCTIONS Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions

@清晰掉 qsort()

qsort函数描述: http://www.cnblogs.com/sooner/archive/2012/04/18/2455011.html qsort()函数实现: /*** *qsort.c - quicksort algorithm; qsort() library function for sorting arrays * Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: * To implem

David MacKay:用信息论解释 &#39;快速排序&#39;、&#39;堆排序&#39; 本质与差异

这篇文章是David MacKay利用信息论,来对快排.堆排的本质差异导致的性能差异进行的比较. 信息论是非常强大的,它并不只是一个用来分析理论最优决策的工具. 从信息论的角度来分析算法效率是一件很有趣的事,它给我们分析排序算法带来了一种新的思路. 运用了信息论的概念,我们很容易理解为什么快排的速度那么快,以及它的缺陷在哪里. 由于个人能力不足,对于本文的理解可能还是有点偏差. 而且因为翻译的困难,这篇译文有很多地方并没有翻译出来,还是使用了原文的句子. 所以建议大家还是阅读原文Heapsort

回调函数到底是怎么一回事?

今天看到回调函数,有点迷糊,找了好多搜索引擎的资料,都不是让我很能理解,看了<c和指针>我才明白了. 简单描述一下什么是回调函数: 用户把一个函数指针作为参数传递给其他函数,后者将"回调"用户的函数.如果函数可以再不同的时间执行不同类型的工作或者执行只能由函数调用者定义的工作,都可以使用回调函数. 回调函数无法知道比较的值的类型,所以参数的类型被声明为void*.表示一个指向未知类型的指针. 可以通过函数指针来实现回调函数.一个指向回调函数的指针作为参数传递给另一个函数,后