一个排序的时间间隔 [英] Sorting a time intervals

查看:125
本文介绍了一个排序的时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给在两个阵列的形式的时间间隔。

I am giving a time interval in the form of two arrays.

A[0]=  2  B[0]=3
A[1]=  9  B[1]=11
A[2] = 5 B[2]=6
A[3] = 3 B[3]=10

我想在开始时间,即基础知识的区间进行排序

I want to sort the interval on the basics of starting time i.e.

(2,3) , (3,10) ,(5,6) ,(9,11)

难道我必须做出这样的结构。或者它可以是直进行。

Does i have to make a structure of this. or it can be done straight.

推荐答案

可以直接做的,因为你不显示什么你试过到目前为止,我只是给你的算法:

It can be done straight, since you dont show what have you tried so far I just give you the algorithm:

for j = 1 to n
    for i = i+1 to n
        if(A[i]>A[j]){
            swap(A[i],A[j])
            swap(B[i],B[j])
        }

您可以轻松地将其转换为Java code。

you can easily convert it to java code.

这种算法是布雷排序,如果你想更好的算法使用维基链接,以提高你的时间

this algorithm is buble sort if you want better algorithm use this wiki link to improve your time.

由于DWB想在这里是归并排序完整的Java code,它做你想做的。我得到了归并排序算法从这里和修改,以满足您的需要。你也可以看到工作版本的 Ideone

As DwB want here is merge sort full java code that do what you want. I got merge sort algorithm from here and modify to satisfy your need. also you could see the working version on Ideone

归并排序:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    private int[] A;
    private int[] B;
    private int[] helperA;
    private int[] helperB;

    private int length;

    public static void main (String[] args){
        int[] As = {2,9,5,3};
        int[] Bs = {3,11,6,10};
        new Ideone().sort(As,Bs);
    }

    public void sort(int[] As , int[] Bs) {
        A = As;
        B = Bs;
        length = A.length;
        this.helperA = new int[length];
        this.helperB = new int[length];
        mergesort(0, length - 1);
        for(int i = 0 ; i<length ; i++)
        System.out.println("(" + A[i] + "," + B[i]+  ")");
    }

    private void mergesort(int low, int high) {
        // check if low issmaller then high, if not then the array is sorted
        if (low < high) {
        // Get the index of the element which is in the middle
        int middle = low + (high - low) / 2;
        // Sort the left side of the array
        mergesort(low, middle);
        // Sort the right side of the array
        mergesort(middle + 1, high);
        // Combine them both
        merge(low, middle, high);
        }
    }

    private void merge(int low, int middle, int high) {

        // Copy both parts into the helper array
        for (int i = low; i <= high; i++) {
            helperA[i] = A[i];
            helperB[i] = B[i];
        }

        int i = low;
        int j = middle + 1;
        int k = low;
        // Copy the smallest values from either the left or the right side back
        // to the original array
        while (i <= middle && j <= high) {
            if (helperA[i] <= helperA[j]) {
                A[k] = helperA[i];
                B[k] = helperB[i];
                i++;
            } else {
                A[k] = helperA[j];
                B[k] = helperB[j];
                j++;
            }
            k++;
        }
        // Copy the rest of the left side of the array into the target array
        while (i <= middle) {
            A[k] = helperA[i];
            B[k] = helperB[i];
            k++;
            i++;
        }
    }
}

这篇关于一个排序的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆