排序算法及先进先出等简单程序

1. 快速/冒泡排序

public class OrderTest {

	public static void main(String[] args) {
		int[] array = {2,1,343,2,43,431,7,49};
		// 冒泡排序
		//bubbleSort(array);
		//快速排序
		quickSort(array,0,7);
	}
	
	public static void bubbleSort(int[] array) {
		
		int temp;
		for(int i=0; i< array.length; i++) {
			for(int j=0;j<array.length-1-i;j++) {
				if (array[j]>array[j+1]) {
					temp = array[j];
					array[j]=array[j+1];
					array[j+1]= temp;
				}
			}
		}
		System.out.println(Arrays.toString(array));
	}

	public static void quickSort(int[] array,int low,int high) {
		int i,j,temp,t;
		if (low > high) {
			return;
		}
		i = low;
		j = high;
		temp = array[i];
		while(i < j) {
			while(array[j] >= temp && j > i) {
				j--;
			}
			
			while(array[i] <= temp && j > i) {
				i++;
			}
			
			if (j>i) {
				t = array[j];
				array[j] = array[i];
				array[i] = t;
			}
		}
		
		array[low] = array[i];
		array[i] = temp;
		System.out.println(temp + Arrays.toString(array));
		quickSort(array, low, j-1);
		quickSort(array, i+1, high);
	}
}

2. 杨辉三角

public class Triangle {
	
	private static Scanner sc;
	private static int n;
	private static int[][] triangleArray;
	
	public static void main(String[] args) {

		sc = new Scanner(System.in);
		n = sc.nextInt();
		triangleArray = new int[n][n];
		
		for(int i=0; i<n; i++) {
			triangleArray[i][0] = triangleArray[i][i] = 1;
			for(int j=1;j<i;j++) {
				triangleArray[i][j] =triangleArray[i-1][j-1] +triangleArray[i-1][j];
			}
		}
		display();
	}
	
	
	private static void display() {
		int i, j;
		for(i = 0; i < n; i++) {
			// 前面空格个数
			for(j = n - i - 1; j > 0; j--) {
				System.out.print("  ");
			}
			// 每个数占3格,且每个数之间空1格
			for(j = 0; j < i; j++) {
				System.out.printf("%3d ", triangleArray[i][j]);
			}
			// 最后1个数,换行
			System.out.printf("%3d\n", triangleArray[i][j]);
		}		
	}

}

3.先进先出队列

public class Queue {
	
	  private int maxSize;
	  private Object[] queueArray;
	  private int front;
	  private int rear;
	  private int size;
	  
	  public Queue(int length) {
		  maxSize = length;
		  queueArray = new Object[maxSize];
		  front = 0;
		  rear = -1;
		  size = 0;
	  }
	  
	  public boolean isFull(){
		return (rear + 2 == front || front + maxSize -2 == rear);
	  }
	  public boolean isEmpty(){
		return (rear + 1 == front || front + maxSize -1 == rear);  
	  }
	  
	  public String enQueue(String str) {
		if (isFull()) {
			throw new RuntimeException("队列已满," + str + " 不能入队!");
		}
		queueArray[++rear] = str;
		size++;
		
		return str;
	  }
	  
	  public String deQueue() {
		  if (isEmpty()) {
			  throw new RuntimeException("队列为空," +  " 不能出队!");
			}
		  String str = (String) queueArray[front++];
		  size--;
		  
		return str;
	  }
	  
	  public int queueSize() {
		  return size;
	  }
	public static void main(String[] args) {
		Queue queue = new Queue(5);
		queue.enQueue("a");
		queue.enQueue("b");
		queue.enQueue("c");
		queue.enQueue("d");
		//queue.enQueue("e");
		
		System.out.println("队列是否为空: " + queue.isEmpty() + "  队列是否满: " + queue.isFull());
		System.out.println("队列大小:" + queue.queueSize());
		
		int size = queue.queueSize();
		for(int i = 0; i < size; i++){
			String str = queue.deQueue();
			System.out.print(str + " ");
		}

		
	}

}

以下是一个简单先进先出算法的程序示例,用于模拟进程调度: ```c #include <stdio.h> #include <stdlib.h> #define MAX_PROCESS 10 struct process { int pid; // 进程ID int arrival_time; // 到达时间 int burst_time; // 执行时间 }; int compare_arrival_time(const void *a, const void *b) { const struct process *pa = a; const struct process *pb = b; return pa->arrival_time - pb->arrival_time; } int main() { struct process processes[MAX_PROCESS]; int n; printf("请输入进程数(最多 %d 个):", MAX_PROCESS); scanf("%d", &n); if (n > MAX_PROCESS) { printf("超过最大进程数!\n"); return 1; } for (int i = 0; i < n; i++) { printf("请输入第 %d 个进程的到达时间和执行时间:", i + 1); scanf("%d%d", &processes[i].arrival_time, &processes[i].burst_time); processes[i].pid = i + 1; } // 按到达时间排序 qsort(processes, n, sizeof(struct process), compare_arrival_time); int time = 0; // 当前时间 int waiting_time = 0; // 等待时间总和 for (int i = 0; i < n; i++) { printf("第 %d 个进程执行,进程ID:%d\n", i + 1, processes[i].pid); waiting_time += time - processes[i].arrival_time; // 计算等待时间 time += processes[i].burst_time; // 更新当前时间 } printf("平均等待时间:%f\n", (float)waiting_time / n); return 0; } ``` 在这个程序中,我们定义了一个 `process` 结构体来表示进程,它包含进程ID、到达时间和执行时间三个属性。我们先让用户输入进程数和每个进程的到达时间和执行时间,然后按照到达时间排序,依次执行每个进程,并计算出所有进程的平均等待时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值