查看: 2905| 回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

Microsoft : One special sort problem

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
There is an array of odd and even numbers. Now, sort them in such a way that the top portion of
the array contains odd numbers, bottom portion contains even numbers. The odd numbers are to be sorted
in descending order and the even numbers in ascending order. You are not allowed to use any extra array
and it has to use a conventional sorting mechanism and should not do any pre or post processing.

给出最"neat"的解决办法

上一篇:Microsoft : Create two evenly balanced teams for a game of soccer.
下一篇:Microsoft : find the next biggest node in a BST
🔗
Roquin 2011-5-29 23:53:33 | 只看该作者
全局:
如果全是正数的话,比较的时候奇数加个符号,按升序排就可以了吧
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-5-31 13:14:36 | 只看该作者
全局:
修改正常的比较逻辑:

奇数比偶数小,奇数中数值上小的比大的大,偶数正常
代码:

class CLess
{
public:
        bool operator() (int a, int b)
        {
                if(a%2 == 0 && b%2 == 1)
                        return false;

                if(a%2 == 1 && b%2 == 0)
                        return true;

                if (a%2 == 0 && b%2 == 0)
                        return a>b;

                return a<b;
        }
};

sort(a, a+n, CLess());
回复

使用道具 举报

🔗
xiaozhuzi27 2012-1-30 11:58:31 | 只看该作者
全局:
不能有pre or post processing。。。难道是奇偶和排序在遍历的时候同时进行么?
我觉得先把奇偶分出来挺靠谱的啊。。。
回复

使用道具 举报

🔗
wwwzj30 2012-2-1 06:33:57 | 只看该作者
全局:
Use quick sort. When comparing numbers any odd number is presented as its negative. O(N log N)
This is equivalent to:
1. Preprocess an array so that all odd numbers are negative to the original
2. Sort the whole array
3. Post-process the array so that all odd numbers are negative to the current (which are all negative)

For example
1234567
when comparing, all odd numbers are in its negative presentation
-1 2 -3 4 -5 6 -7
After Sort:
-7 -5 -3 -1 2 4 6
But the number itself is not actually inverted
7 5 4 1 2 4 6

It fits the requirement of top portion of the array contains odd numbers, bottom portion contains even numbers. The odd numbers are to be sorted in descending order and the even numbers in ascending order.
回复

使用道具 举报

🔗
ilnlh 2012-2-27 13:18:59 | 只看该作者
全局:
bubble sort (sentinel at index 0):
  1. def bsort(l):
  2.   n = len(l) - 1
  3.   while n > 1:
  4.     l[0] = l[1]
  5.     i = 1
  6.     while i < n:
  7.       left = l[i]
  8.       right = l[i + 1]
  9.       if left % 2 == 0 and right % 2 == 1 or \
  10.           (left % 2 == 0 and right % 2 == 0 and right < left) or \
  11.           (left % 2 == 1 and right % 2 == 1 and left < right):
  12.         l[0] = l[i + 1]
  13.         l[i + 1] = l[i]
  14.         l[i] = l[0]
  15.       i += 1
  16.     if l[0] == l[1]:
  17.       break
  18.     n -= 1
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表