zoukankan      html  css  js  c++  java
  • 数组左边奇数右边偶数算法O(n)

    #include<stdio.h>
    #include <iostream>
    #include <algorithm>
    #include<time.h>
    #include<stdlib.h>
    #define SIZE 10
    using namespace std;
    void adjust(int array[],int n);	//调整数组元素的函数声明
    void main()
    {
    	int array[SIZE],i;
    	srand(time(NULL));		//获取随机种子
    	for(i=0;i<SIZE;i++)		//为数组元素赋值,以生成数组
    	{
    		array[i]=rand()%100+1;
    	}
    	printf("Before adjust array,the array's elements are:
    ");
    	for(i=0;i<SIZE;i++)
    	{
    		printf("%d	",array[i]);
    	}
    	printf("
    ");
    	adjust(array,SIZE);	//调用调整数组元素的函数
    	printf("After adjust array,the array's elements are:
    ");
    	for(i=0;i<SIZE;i++)
    	{
    		printf("%d	",array[i]);
    	}
    	printf("
    ");
    }
    void adjust(int array[],int n)		//调整数组元素的函数的定义
    {
    	int i=0,j=n-1,temp;
    	while(i<j)
    	{
    		if(array[i]%2!=0)i++;
    		if(array[j]%2==0)j--;
    		if(i<j)		
    			swap(array[i],array[j]);
    	}
    }
    
  • 相关阅读:
    文件的上传
    JSP基础知识
    AJAX
    Listener
    Filter(一)
    session
    网络爬虫(9)-xpath解析
    网络爬虫(8)-正则表达式
    网络爬虫(7)-beautifulSoup解析库
    网络爬虫(6)-Requests库
  • 原文地址:https://www.cnblogs.com/yonglin1998/p/11780804.html
Copyright © 2011-2022 走看看