# Sorting_Searching 读入数字流求rank @CareerCup

package Sorting_Searching;

import CtCILibrary.AssortedMethods;

/**
* Imagine you are reading in a stream of integers. Periodically, you wish to be
* able to look up the rank of a number x (the number of values less than or
* equal to x). Implement the data structures and algorithms to support these
* operations. That is, implement the method track(int x), which is called
* when each number is generated, and the methodgetRankOfNumber (int x), which
* returns the number of values less than or equal to x (not including x
* itself).
*
* EXAMPLE
Stream (in order of appearance): 5,  1,  4,  4,  5,  9,  7,  13,  3
getRankOfNumber(l) = 0
getRankOfNumber(3) = 1
getRankOfNumber(4) = 3

* 不断地读入数字流，要求实现track和getRankOfNumber方法。
*
*/
public class S11_8 {

private static RankNode root = null;

// track就是插入BST的过程
public static void track(int number) {
if (root == null) {
root = new RankNode(number);
} else {
root.insert(number);
}
}

// 得到rank就是得到有多少个比number小
public static int getRankOfNumber(int number) {
return root.getRank(number);
}

public static void main(String[] args) {
int size = 5;
int[] list = AssortedMethods.randomArray(size, -10, 10);
for (int i = 0; i < list.length; i++) {
track(list[i]);
}

int[] tracker = new int[size];
for (int i = 0; i < list.length; i++) {
int v = list[i];
int rank1 = root.getRank(list[i]);
tracker[rank1] = v;
}

for (int i = 0; i < tracker.length - 1; i++) {
if (tracker[i] != 0 && tracker[i + 1] != 0) {
if (tracker[i] > tracker[i + 1]) {
System.out.println("ERROR at " + i);
}
}
}

//		System.out.println("Array: " + AssortedMethods.arrayToString(list));
//		System.out.println("Ranks: " + AssortedMethods.arrayToString(tracker));
for(int i : list){
System.out.println(i + ": " + getRankOfNumber(i));
}
}

static class RankNode {
public int left_size = 0;
public RankNode left;
public RankNode right;
public int data = 0;

public RankNode(int d) {
data = d;
}

public void insert(int d) {
if (d <= data) {
if (left != null) {
left.insert(d);
} else {
left = new RankNode(d);
}
left_size++;
} else {
if (right != null) {
right.insert(d);
} else {
right = new RankNode(d);
}
}
}

public int getRank(int d) {
if (d == data) {
return left_size;
} else if (d < data) {
if (left == null) {
return -1;
} else {
return left.getRank(d);
}
} else {
int right_rank = right == null ? -1 : right.getRank(d);
if (right_rank == -1) {
return -1;
} else {
return left_size + 1 + right_rank;
}
}
}
}
}

## searching, sorting (part 1 - selection sort, bubble sort)

• 2014年03月29日 14:44
• 297KB
• 下载

## Computer Science - Sorting &amp; Searching Algorithm Book

• 2008年03月19日 23:07
• 1.04MB
• 下载

## Algorithms in C++, Parts 1-4: Fundamentals, Data Structures, Sorting, Searching

• 2012年01月28日 18:39
• 11.45MB
• 下载

## Graph Alg Heaps Sorting Searching

• 2011年08月06日 03:41
• 153KB
• 下载

## uva算法入门经典题解导航 - Volume 1. Elementary Problem Solving - Sorting/Searching

Title Total Submissions / Solving % Total Users / Solving % 340 - Master-Mind Hints 7611 51....

## 计算机程序设计与艺术第三卷：排序与查找Sorting and Searching(中英)

• 2015年11月04日 22:01
• 49.26MB
• 下载

## Algorithms in C++, Parts 1–4 Fundamentals, Data Structure, Sorting, Searching, Third Edition

• 2010年03月18日 12:52
• 5.84MB
• 下载

## Data Structures And Algorithms (Vol 1 Sorting And Searching)

• 2008年10月19日 15:37
• 2.04MB
• 下载

举报原因： 您举报文章：Sorting_Searching 读入数字流求rank @CareerCup 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)