The-ith-Element

Brief: the-ith-element,given a array A with n element , return the i-th element of A.  A(n,i)

this problem can be solved using quick-sort idear, every time we choose a pivot ,after rearrangement, we know the pivot is the i-th element of A, so we can fixed this problem as the follow action.

i) choose  pivot, rearrangement array, get the pivot index of j;

ii) if j > i , then recursion the 1-th partition , A(1-th , i);

iii)if j < i, then recursion the 2-th partition, A(2-th, i-j);

int element(int *array, int left, int right, int index)
{
	if(left >= right)
		return array[left];
	int pivot = array[left];
	int i=0, j = 0;

	for(i=left+1,j = left+1; j <=right; ++j)
	{
		if(array[j] < pivot)
			swap(array[i++], array[j]);
	}
	swap(array[left], array[i-1]);

	//pivot is the (i-left)-th element, and pivot‘s index is i-1

	if(index == i-left)
		return array[i-1];
	else if(index < i-left)
		return element(array, left, i-2, index);
	else
		return element(array, i,right, index-i+left);
}

  

时间: 2024-12-08 05:33:38

The-ith-Element的相关文章

121.买卖股票 Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Exam

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit

121. Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Exam

122. Best Time to Buy and Sell Stock II

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times)

【转】Python零碎知识(2):强大的zip

转自:http://www.cnblogs.com/BeginMan/archive/2013/03/14/2959447.html 这篇博文讲的挺好的 一.代码引导 首先看这一段代码: 1 >>> name=('jack','beginman','sony','pcky') 2 >>> age=(2001,2003,2005,2000) 3 >>> for a,n in zip(name,age): 4 print a,n 5 6 输出: 7 jac

122. 买卖股票 求最大收益2 Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

Singly Linked List

Singly linked list storage structure:typedef struct Node{ ElemType data; struct Node *next;}Node; typedef struct Node *LinkList; LinkedList without head node: LinkedList with head node: Operations: /*check the size of link list.*/int ListLength(LinkL

python学习之函数

1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回None 2.2.return后,函数下面的语句不会被执行,中断函数操作 2.3.return个什么东西都行,哪怕是个列表..... 3.pycharm使用断点调试的话,需要用debug模式(向右小箭头的小虫子) 4.参数: 默认参数必须写在后边 def aaa(a1, a2 = 1): pass//

python zip()

>>> help(zip) Help on built-in function zip in module __builtin__: zip(...) zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. Th

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct