Bubblesort and oblivious

Problem 12. (Difficulty 2) Bubblesort and oblivious merge-sort each give a sequence of
compare-exchange operations which sorts any array A[0..3]. Write down both sequences.
Problem 13. (Difficulty 4) For n distinct elements x1, x2, . . . , xn with positive weights
w1, w2, . . . , wn such that Pn
i=1 wi = 1, define the weighted median to be an index k satisfying
P
i:xi<xk
wi ≤ 1/2 and P
i:xi>xk
wi ≤ 1/2. Give an O(n) algorithm for finding the
weighted median.
Problem 14. (Difficulty 3) Suppose we are given k sorted lists, each consisting of n elements.
The multimerge problem is to merge the k lists into a single sorted list of length nk. Design an
efficient algorithm for the multimerge problem. Analyze the running time of your algorithm.
The more efficient your algorithm is in terms of its worst-case running time, the more credit
you will get.
2 Homework 1
Problem 15. (Difficulty 3) Return a list of the following functions separated by the symbol
≡or , where f ≡ g means f = Θ(g) and f g means f = O(g). You do not need
to justify your answer. For example, if the functions are log n, n, 5n, 2
n a correct answer is
log n n ≡ 5n 2
n
. All logarithms are in base 2. One point for each correct symbol.
Problem 16. (Difficulty 3) Solve the following recurrences. Give both O(.) and (.).
T(n) = 4T(n/3) + n,
T(n) = 3T(n/4) + n,
T(n) = 3T(n/4) + n
2
.
5
Problem 17. (Difficulty 2) Execute the Quick-Sort algorithm on the following array
[5, 2, 6, 1, 3, 4, 7]
using the last element of the array as a pivot element. Show the contents of the array after
each pass of partition.
Problem 18. (Difficulty 3) Give an algorithm running in time O(n) and space O(1) which
sorts an array of n elements in the range {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. Note that you cannot use
another array of length n for the output because you are only allowed space O(1).
Problem 19. (Difficulty 3) You are given as input an array A[1..n] containing integer
numbers from 1 to n
10. Give an O(n)-time algorithm to determine if there are two numbers
which sum to 0.

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:[email protected]

微信:codinghelp

原文地址:https://www.cnblogs.com/javapythonc/p/10311126.html

时间: 2024-07-30 15:04:34

Bubblesort and oblivious的相关文章

java8 函数式版 冒泡排序(BubbleSort)

package com.doctor.algorithm.sort; import java.util.Arrays; import java.util.function.BiPredicate; /**  * java8 函数式版 冒泡排序(BubbleSort)  *   * @author doctor  *  * @time 2015年4月26日 下午9:31:03  */ public class BubbleSort { public static void main(String[

排序算法SIX:冒泡排序BubbleSort

1 /** 2 *冒泡排序: 3 * 两个两个比较,一轮过后最大的排在了最后面 4 * n个数变为n-1个没排好的数 5 * 再进行一轮 6 * 第二大的排在了倒数第二个 7 * 以此类推 8 * 直到排到第一个为止 9 * 10 * 弄两个循环,相邻两个数比较 11 */ 12 public class BubbleSort 13 { 14 /** 15 *冒泡排序主方法 16 * 17 */ 18 public static void bubbleSort(int[] resouceArr)

bubblesort的常数优化

内容来自TsinghuaX: 30240184X 数据结构(2015秋)这门课的Vector一章,对bubblesort有两次常数优化. 函数原型是这样的: void bubble(Rank lo, Rank hi); void bubbleSort(Rank lo, Rank hi); Rank即向量元素的秩,在此被定义为int型.lo和hi为待排序区间的左.右界桩. 外部通过调用 v.bubbleSort(lo, hi) 来给向量v的区间[lo,hi)排序,而bubbleSort调用bubb

递归方式实现BubbleSort 冒泡排序

冒泡排序算法的运作如下: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.这步做完后,最后的元素会是最大的数. 针对所有的元素重复以上的步骤,除了最后一个. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较. package sort; import java.util.Arrays; import java.util.Random; public class BubbleSort {     public 

数据结构基础(1) --Swap &amp; Bubble-Sort &amp; Select-Sort

Swap的简单实现 //C语言方式(by-pointer): template <typename Type> bool swapByPointer(Type *pointer1, Type *pointer2) { //确保两个指针不会指向同一个对象 if (pointer1 == NULL || pointer2 == NULL) { return false; } if (pointer1 != pointer2) { Type tmp = *pointer1; *pointer1 =

BubbleSort冒泡排序

#include <stdio.h>void BubbleSort(int *a,int n);int main(void){ int arr[10] = {2,4,6,8,0,1,3,5,7,9}; int k; for(k=0;k<10;k++){ if(k==9) printf("%d\n",arr[k]); else printf("%d,",arr[k]); } BubbleSort(arr,10); for(k=0;k<10;k+

js bubbleSort

function bubbleSort(arr) { if (arr.length <= 0) { return arr; } for (var i = 0, len = arr.length - 1; i < len; i++) { for (var j = 0; j < len - 1- i; j++) { if (arr[j] > arr[j + 1]) { var temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp;

冒泡排序BubbleSort

/** * * @author Administrator * 功能:交换式排序之冒泡排序 */ package com.test1; import java.util.Calendar; public class BubbleSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = new int[50000]; for (int i = 0; i < arr.l

Java BubbleSort

package sort.bubble; import java.util.Arrays; public class BubbleSort { public static void main(String[] args) { int[] arr = {6,8,4,7,9,12}; bubbleSort(arr); //bubbleSort2(arr); //bubbleSort1(arr); } /*交换次数最少的冒泡排序 * 冒泡排序,根据最后交换数位置 */ public static vo