「java选举算法」leader选举算法

博主:adminadmin 2022-11-23 12:04:22 70

本篇文章给大家谈谈java选举算法,以及leader选举算法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

请给出java几种排序方法

java常见的排序分为:

1 插入类排序

主要就是对于一个已经有序的序列中,插入一个新的记录。它包括:直接插入排序,折半插入排序和希尔排序

2 交换类排序

这类排序的核心就是每次比较都要“交换”,在每一趟排序都会两两发生一系列的“交换”排序,但是每一趟排序都会让一个记录排序到它的最终位置上。它包括:起泡排序,快速排序

3 选择类排序

每一趟排序都从一系列数据中选择一个最大或最小的记录,将它放置到第一个或最后一个为位置交换,只有在选择后才交换,比起交换类排序,减少了交换记录的时间。属于它的排序:简单选择排序,堆排序

4 归并类排序

将两个或两个以上的有序序列合并成一个新的序列

5 基数排序

主要基于多个关键字排序的。

下面针对上面所述的算法,讲解一些常用的java代码写的算法

二 插入类排序之直接插入排序

直接插入排序,一般对于已经有序的队列排序效果好。

基本思想:每趟将一个待排序的关键字按照大小插入到已经排序好的位置上。

算法思路,从后往前先找到要插入的位置,如果小于则就交换,将元素向后移动,将要插入数据插入该位置即可。时间复杂度为O(n2),空间复杂度为O(1)

package sort.algorithm;

public class DirectInsertSort {

public static void main(String[] args) {

// TODO Auto-generated method stub

int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };

int temp, j;

for (int i = 1; i data.length; i++) {

temp = data[i];

j = i - 1;

// 每次比较都是对于已经有序的

while (j = 0 data[j] temp) {

data[j + 1] = data[j];

j--;

}

data[j + 1] = temp;

}

// 输出排序好的数据

for (int k = 0; k data.length; k++) {

System.out.print(data[k] + " ");

}

}

}

三 插入类排序之折半插入排序(二分法排序)

条件:在一个已经有序的队列中,插入一个新的元素

折半插入排序记录的比较次数与初始序列无关

思想:折半插入就是首先将队列中取最小位置low和最大位置high,然后算出中间位置mid

将中间位置mid与待插入的数据data进行比较,

如果mid大于data,则就表示插入的数据在mid的左边,high=mid-1;

如果mid小于data,则就表示插入的数据在mid的右边,low=mid+1

最后整体进行右移操作。

时间复杂度O(n2),空间复杂度O(1)

package sort.algorithm;

//折半插入排序

public class HalfInsertSort {

public static void main(String[] args) {

int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };

// 存放临时要插入的元素数据

int temp;

int low, mid, high;

for (int i = 1; i data.length; i++) {

temp = data[i];

// 在待插入排序的序号之前进行折半插入

low = 0;

high = i - 1;

while (low = high) {

mid = (low + high) / 2;

if (temp data[mid])

high = mid - 1;

else

// low=high的时候也就是找到了要插入的位置,

// 此时进入循环中,将low加1,则就是要插入的位置了

low = mid + 1;

}

// 找到了要插入的位置,从该位置一直到插入数据的位置之间数据向后移动

for (int j = i; j = low + 1; j--)

data[j] = data[j - 1];

// low已经代表了要插入的位置了

data[low] = temp;

}

for (int k = 0; k data.length; k++) {

System.out.print(data[k] + " ");

}

}

}

四 插入类排序之希尔排序

希尔排序,也叫缩小增量排序,目的就是尽可能的减少交换次数,每一个组内最后都是有序的。

将待续按照某一种规则分为几个子序列,不断缩小规则,最后用一个直接插入排序合成

空间复杂度为O(1),时间复杂度为O(nlog2n)

算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。

package sort.algorithm;

public class ShellSort {

public static void main(String[] args) {

int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };

double d1 = a.length;

int temp = 0;

while (true)

{

//利用这个在将组内倍数减小

//这里依次为5,3,2,1

d1 = Math.ceil(d1 / 2);

//d为增量每个分组之间索引的增量

int d = (int) d1;

//每个分组内部排序

for (int x = 0; x d; x++)

{

//组内利用直接插入排序

for (int i = x + d; i a.length; i += d) {

int j = i - d;

temp = a[i];

for (; j = 0 temp a[j]; j -= d) {

a[j + d] = a[j];

}

a[j + d] = temp;

}

}

if (d == 1)

break;

}

for (int i = 0; i a.length; i++)

System.out.print(a[i]+" ");

}

}

五 交换类排序之冒泡排序

交换类排序核心就是每次比较都要进行交换

冒泡排序:是一种交换排序

每一趟比较相邻的元素,较若大小不同则就会发生交换,每一趟排序都能将一个元素放到它最终的位置!每一趟就进行比较。

时间复杂度O(n2),空间复杂度O(1)

package sort.algorithm;

//冒泡排序:是一种交换排序

public class BubbleSort {

// 按照递增顺序排序

public static void main(String[] args) {

// TODO Auto-generated method stub

int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20, 13, 100, 37, 16 };

int temp = 0;

// 排序的比较趟数,每一趟都会将剩余最大数放在最后面

for (int i = 0; i data.length - 1; i++) {

// 每一趟从开始进行比较,将该元素与其余的元素进行比较

for (int j = 0; j data.length - 1; j++) {

if (data[j] data[j + 1]) {

temp = data[j];

data[j] = data[j + 1];

data[j + 1] = temp;

}

}

}

for (int i = 0; i data.length; i++)

System.out.print(data[i] + " ");

}

}

我编的图书馆管理系统,想三台电脑通过局域网共享该系统,请问哪位高手知道怎么实现?请高手赐教!谢谢!

简单点的,以数据库-服务器-多客户端形式组建。具体的开发可以用C的RPC,或者JAVA的RMI。然后如果想要三个电脑互相热备,可以使用peer2peer对等模式,然后以选举方式选出leader。选举算法有很多,你可以上网搜下,建议采用Bully算法,因为比较简单。

如果会J2EE的话就更加方便了,只需要开发服务器端,客户端用IE就可以代替。

当然上面的实现都是从头开始做的最佳办法,可以这么做,不过,我想你的管理系统应该是数据库+客户端形式的,如果你能做到实现客户端以“事务”形式提交数据,也就是一个客户端在“事务”中时,禁止其他客户端提交更改(当然这样比较低效,但是我想你的系统应该是习作,效率并不是太大问题),你也不必太拘泥于分布式实现。把客户端拷贝给不同的电脑,通过自己链接数据库,“事务”式确保不会有互相干扰,这样的实现方式,也是可以的。

总之,实现事务,是你必须关心的要点,至于需要事务实现到什么程度,则是看你的系统的需求如何了。

以上全是dnastar原创,转载请注明,谢谢。

Java的排序算法有哪些

java的排序大的分类可以分为两种:内排序和外排序。在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序。下面讲的排序都是属于内排序。

1.插入排序:直接插入排序、二分法插入排序、希尔排序。

2.选择排序:简单选择排序、堆排序。

3.交换排序:冒泡排序、快速排序。

4.归并排序

5.基数排序

用java编程一道选举题目,要求用数组!偶是小白,讲解时记得每步加注释哦!麻烦大神了,谢谢!

//下次私聊吧

//add My QQ 694374922

public class Test {

public static void main(String [] args) {

String [] voteObj = {"A","B","C"};//数组储存候选人

Scanner input = new Scanner(System.in);//构造Scanner对象,用于接收用户输入

//下面打印一个菜单

System.out.println("****************欢迎进入投票菜单程序*****************");

System.out.println("请输入对应的序号给候选人投票:");

System.out.println("1. A");

System.out.println("2. B");

System.out.println("3. C");

System.out.println("**********************************************");

System.out.println("您的选择是:");//提示用户输入

int choice = input.nextInt();//接收用户输入

switch (choice) {//判断用户输入

case 1:

System.out.println("您已成功为A投票!");//打印结果

break;//程序退出

case 2:

System.out.println("您已成功为B投票!");

break;

case 3:

System.out.println("您已成功为C投票!");

break;

default:

System.out.println("抱歉,您的输入错误!");

}

}

}

私聊我

java面试有哪些算法

面试-java算法题:

1.编写一个程序,输入n,求n!(用递归的方式实现)。

public static long fac(int n){ if(n=0) return 0; else if(n==1) return 1; else return n*fac(n-1);

} public static void main(String [] args) {

System.out.println(fac(6));

}

2.编写一个程序,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

public static void main(String [] args) { int i, j, k; int m=0; for(i=1;i=4;i++) for(j=1;j=4;j++) for(k=1;k=4;k++){ if(i!=jk!=ji!=k){

System.out.println(""+i+j+k);

m++;

}

}

System.out.println("能组成:"+m+"个");

}

3.编写一个程序,将text1.txt文件中的单词与text2.txt文件中的单词交替合并到text3.txt文件中。text1.txt文件中的单词用回车符分隔,text2.txt文件中用回车或空格进行分隔。

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

public class text{

public static void main(String[] args) throws Exception{

String[] a = getArrayByFile("text1.txt",new char[]{'\n'});

String[] b = getArrayByFile("text2.txt",new char[]{'\n',' '});

FileWriter c = new FileWriter("text3.txt");

int aIndex=0; int bIndex=0;

while(aIndexa.length){

c.write(a[aIndex++] + "\n");

if(bIndexb.length)

c.write(b[bIndex++] + "\n");

}

while(bIndexb.length){

c.write(b[bIndex++] + "\n");

}

c.close();

}

public static String[] getArrayByFile(String filename,char[] seperators) throws Exception{

File f = new File(filename);

FileReader reader = new FileReader(f);

char[] buf = new char[(int)f.length()];

int len = reader.read(buf);

String results = new String(buf,0,len);

String regex = null;

if(seperators.length 1 ){

regex = "" + seperators[0] + "|" + seperators[1];

}else{

regex = "" + seperators[0];

}

return results.split(regex);

}

}

4.639172每个位数上的数字都是不同的,且平方后所得数字的所有位数都不会出现组成它自身的数字。(639172*639172=408540845584),类似于639172这样的6位数还有几个?分别是什么?

这题采用的HashMap结构判断有无重复,也可以采用下题的数组判断。

public void selectNum(){

for(long n = 100000; n = 999999;n++){

if(isSelfRepeat(n)) //有相同的数字,则跳过

continue;

else if(isPingFangRepeat(n*n,n)){ //该数的平方中是否有与该数相同的数字

continue;

} else{ //符合条件,则打印 System.out.println(n);

}

}

} public boolean isSelfRepeat(long n){

HashMapLong,String m=new HashMapLong,String(); //存储的时候判断有无重复值

while(n!=0){ if(m.containsKey(n%10)){ return true;

} else{

m.put(n%10,"1");

}

n=n/10;

} return false;

} public boolean isPingFangRepeat(long pingfang,long n){

HashMapLong,String m=new HashMapLong,String(); while(n!=0){

m.put(n%10,"1");

n=n/10;

} while(pingfang!=0){ if(m.containsKey(pingfang%10)){ return true;

}

pingfang=pingfang/10;

} return false;

} public static void main(String args[]){ new test().selectNum();

}

5.比如,968548+968545=321732732它的答案里没有前面两个数里的数字,有多少这样的6位数。

public void selectNum(){

for(int n = 10; n = 99;n++){

for(int m = 10; m = 99;m++){ if(isRepeat(n,m)){ continue;

} else{

System.out.println("组合是"+n+","+m);

}

}

}

} public boolean isRepeat(int n,int m){ int[] a={0,0,0,0,0,0,0,0,0,0}; int s=n+m; while(n!=0){

a[n%10]=1;

n=n/10;

} while(m!=0){

a[m%10]=1;

m=m/10;

} while(s!=0){ if(a[s%10]==1){ return true;

}

s=s/10;

} return false;

} public static void main(String args[]){ new test().selectNum();

}

6.给定String,求此字符串的单词数量。字符串不包括标点,大写字母。例如 String str="hello world hello hi";单词数量为3,分别是:hello world hi。

public static void main(String [] args) { int count = 0;

String str="hello world hello hi";

String newStr="";

HashMapString,String m=new HashMapString,String();

String [] a=str.split(" "); for (int i=0;ia.length;i++){ if(!m.containsKey(a[i])){

m.put(a[i],"1");

count++;

newStr=newStr+" "+a[i];

}

}

System.out.println("这段短文单词的个数是:"+count+","+newStr);

}

7.写出程序运行结果。

public class Test1 { private static void test(int[]arr) { for (int i = 0; i arr.length; i++) { try { if (arr[i] % 2 == 0) { throw new NullPointerException();

} else {

System.out.print(i);

}

} catch (Exception e) {

System.out.print("a ");

} finally {

System.out.print("b ");

}

}

}

public static void main(String[]args) { try {

test(new int[] {0, 1, 2, 3, 4, 5});

} catch (Exception e) {

System.out.print("c ");

}

}

}

运行结果:a b 1b a b 3b a b 5b

public class Test1 { private static void test(int[]arr) { for (int i = 0; i arr.length; i++) { try { if (arr[i] % 2 == 0) { throw new NullPointerException();

} else {

System.out.print(i);

}

}

finally {

System.out.print("b ");

}

}

}

public static void main(String[]args) { try {

test(new int[] {0, 1, 2, 3, 4, 5});

} catch (Exception e) {

System.out.print("c ");

}

}

}

运行结果:b c

8.单词数

统计一篇文章里不同单词的总数。

Input

有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

Output

每组值输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input

you are my friend

#

Sample Output

4

public static void main(String [] args) {

ListInteger countList=new ArrayListInteger(); int count;

HashMapString,String m;

String str; //读取键盘输入的一行(以回车换行为结束输入) String[] a;

Scanner in=new Scanner(System.in);

while( !(str=in.nextLine()).equals("#") ){

a=str.split(" ");

m=new HashMapString,String();

count = 0; for (int i=0;ia.length;i++){ if(!m.containsKey(a[i]) (!a[i].equals(""))){

m.put(a[i],"1");

count++;

}

}

countList.add(count);

}s for(int c:countList)

System.out.println(c);

}

java中的算法,一共有多少种,哪几种,怎么分类。

就好比问,汉语中常用写作方法有多少种,怎么分类。

算法按用途分,体现设计目的、有什么特点

算法按实现方式分,有递归、迭代、平行、序列、过程、确定、不确定等等

算法按设计范型分,有分治、动态、贪心、线性、图论、简化等等

作为图灵完备的语言,理论上”Java语言“可以实现所有算法。

“Java的标准库'中用了一些常用数据结构和相关算法.

像apache common这样的java库中又提供了一些通用的算法

关于java选举算法和leader选举算法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。