正负数排列(用链表和数组实现)

【问题描述】

一个未排序整数数组,有正负数,重新排列使负数排在正数前面。并且要求不改变原来的正负数之间相对顺序。比如输入1、7、-5、9、-12、15,输出-5、-12、1、7、9、15。

【基本要求】

多种算法解决这个问题,并探讨有无时间复杂度O(N),空间复杂度O(1)的算法。

public class ch01 {
    public void operate(int maxSize,int[] numbs){
        int j=0;                          //为了把负数移到前面而设置的
        int m = 0,i,temp,k=0;             //m是为了寻找负数的具体位置,i是为了在一个范围内寻找负数,k是为了在一个范围内移动数字而设置
        for(i=m;i<maxSize;i++){           //一开始m是0,从0开始遍历这个序列
            if(numbs[i]<0){               //一旦发现该序列的其中一个元素小于0
                m=i;                      //让m来标记该元素的位置
                temp=numbs[i];            //把该元素暂存为temp
                for(i=m;i>k;i--){         //把该元素前的元素一个个往后移
                    numbs[i]=numbs[i-1];
                }
                k++;                      //第一次发现一个元素为负数,这个元素最终会被放进i=0的位置,k++是为了以后移动元素时不会干扰
                numbs[j]=temp;            //到已固定位置的元素
                j++;                      //j++是为了继续存放负数的元素
            }
        }
        System.out.println("用数组进行实现,排序完的序列为:");
        for(i=0;i<maxSize;i++){
            System.out.print(numbs[i]+" ");
        }
        System.out.println();
    }
}
public class ch02 {
    public static class Node{
        int data;
        Node next;

        public Node(){}

        public Node(int data){
            this.data=data;
        }
    }

    public void operate(int[] numbs){
        Node head=new Node();
        Node cur=head;                            //用来存放数组的元素
        Node p=head;                              //用来寻找小于0的元素
        Node insert=new Node();
        Node n=insert;                            //用来把小于0的元素存进数组中

        for(int numb:numbs){                      //用来把数组中的元素存进链表中
            Node node=new Node(numb);
            cur.next=node;
            cur=node;
        }

            while(p.next!=null){
            if(p.next.data<0){                    //把小于0的元素存进一个链表
                Node node=new Node(p.next.data);
                n.next=node;
                n=node;

                if(p.next.next==null){            //当数组中最后一个元素为负数时,需判断以便把该数存进专门存放负数的链表
                    p.next=null;
                    break;
                }

                p.next=p.next.next;               //把是负数的元素从链表断开
                continue;
            }
            p=p.next;                             //p向后移动
        }

            n.next=head.next;                     //存放负数的链表连接存放正数的链表
            head.next=insert.next;                //头结点连接一整个链表

        System.out.println("用链表进行实现,排序完的序列为:");
        while(head.next!=null){
            head=head.next;
            System.out.print(head.data+" ");
        }
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[] args) throws IOException {
        String inputStr;
        boolean mark=false;
        int a=0;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        do {
            try {
                System.out.println("请输入你要输入的整数序列的个数:");
                inputStr = in.readLine();
                a = Integer.parseInt(inputStr);
                if (a <= 0) {
                    throw new NegativeIntegerException();
                }
                mark=true;
            } catch (NumberFormatException | NegativeIntegerException e) {
                System.out.println("非法输入!!!请输入一个正整数!");
            }
        }while(!mark);

        mark=false;
        int[] numbs = new int[a];
        do {
            try {
                System.out.println("请输入一个整数序列,敲回车隔开:");
                for (int i=0;i<a;i++) {
                    inputStr = in.readLine();
                    numbs[i] = Integer.parseInt(inputStr);
                }
                mark = true;
            } catch (NumberFormatException e) {
                System.out.println("非法输入!!!请输入一个正整数!");
            }
        }while (!mark);

        ch01 c=new ch01();
        c.operate(a,numbs);

        ch02 c1=new ch02();
        c1.operate(numbs);
    }
    private static class NegativeIntegerException extends Throwable {
    }
}

 

 

 

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值