Algorithms used in algorithm analysis Class 1

Source: Internet
Author: User
Tags ack integer division

Ackerman
Package FIRSTP;
Import Java.util.Scanner;
public class Ackerman {
public static int ack (int n,int m)
{
if (n==0&&m>=0) return 1;
if (n==1&&m==0) return 2;
if (n>=2&&m==0) return n+2;
if (n>=1&&m>=1) return ack (ACK (n-1,m), m-1);
return-1;
}
public static void Main (string[] args)
{
TODO auto-generated method stubs
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
int M=sc.nextint ();
System.out.println (ACK (n,m));
}
}
Full arrangement
Package FIRSTP;
Import Java.util.Scanner;
public class ALLP {
public static void All (string[] a,int start,int end)
{
if (start==end)
{
for (int i=0;i<=end;i++)
{
System.out.print (a[i]+ "");
}
System.out.println ();
}
else{
for (int i=start;i<=end;i++)
{
String Temp=a[start];
A[start]=a[i];
A[i]=temp;
All (A,start+1,end);
Temp=a[start];
A[start]=a[i];
A[i]=temp;
}
}
}
public static void Main (string[] args)
{
TODO auto-generated method stubs
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
String[] A=new String[n];
for (int i=0;i<n;i++)
{
A[i]=sc.next ();
}
All (a,0,a.length-1);
}
}
Fibonacci
Package FIRSTP;
Import Java.util.Scanner;
public class Fibonacci {
public static int fi (int n)
{
if (n<=2)
{
return 1;
}
Else
{
return fi (n-1) +fi (n-2);
}
}
public static int nfi (int n)
{
if (n<=2)
{
return 1;
}
int n1=1,n2=1,sn=0;
for (int i=0;i<n-2;i++)
{
SN=N1+N2;
N1=N2;
N2=SN;
}
return SN;
}
public static void Main (string[] args)
{
TODO auto-generated method stubs
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
System.out.println (FI (n));
int sum=0;
for (int i=1;i<=n;i++)
{
Sum=nfi (i);
}
SYSTEM.OUT.PRINTLN (sum);
}
}
Merge sort
Package FIRSTP;
Import Java.util.Scanner;
public class Hebing {
Static int[] B;
public static void MergeSort (Int. a[],int Left,int right)
{
if (left<right)
{

int i= (left+right)/2;
MergeSort (A,left,i);
MergeSort (A,i+1,right);
Merge (A,b,left,i,right);
Copy (A,b,left,right);
}
}
static void merge (int x[],int y[],int l,int m,int R)
{
int i=l,j=m+1,k=l;
while ((i<=m) && (j<=r))
{
if (X[i]<=x[j])
Y[k++]=x[i++];
Else
Y[k++]=x[j++];
}
while (I&LT;=M)
Y[k++]=x[i++];
while (J&LT;=R)
Y[k++]=x[j++];
}
static void Copy (int a[],int b[],int m,int N)
{
for (int i=m;i<=n;i++)
A[i]=b[i];
}
public static void Main (string[] args)
{
TODO auto-generated method stubs
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
B=new Int[n];
Int[] A=new Int[n];
for (int i=0;i<a.length;i++)
{
A[i]=sc.nextint ();
}

MergeSort (A, 0, n-1);
for (int i=0;i<a.length;i++)
{
System.out.print (a[i]+ "");
}
}
}
Integer division
Package FIRSTP;
Import Java.util.Scanner;
public class INTP {
static int q (int n,int m)
{
if (n<1| | M&LT;1) return 0;
if (n==1| | M==1) return 1;
if (n<m) return Q (n,n);
if (n==m) return Q (n,n-1) +1;
return Q (n,m-1) +q (n-m,m);
}
public static void Main (string[] args)
{
TODO auto-generated method stubs
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
System.out.println (q (n,n));

}
}
Quick Sort
Package FIRSTP;
Import Java.util.Scanner;
public class Quickp {
public static void Main (string[] args)
{
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
Int[] A=new Int[n];
for (int i=0;i<n;i++)
{
A[i]=sc.nextint ();
}
Quicsort (a,0,a.length-1);
for (int i=0;i<n;i++)
{
System.out.print (a[i]+ "");
}
}
public static void Quicsort (int[] a,int start,int end)
{
if (start<end)
{
int i=start,j=end;
int vot=a[i];
while (I!=J)
{
while (I<j&&vot<=a[j])
j--;
if (I&LT;J)
A[I++]=A[J];
while (I<j&&vot>=a[i])
i++;
if (I&LT;J)
A[j--]=a[i];
}
A[i]=vot;
Quicsort (a,start,j-1);
Quicsort (A,i+1,end);
}
}
}
Two-point Search
Package FIRSTP;
Import Java.util.Arrays;
Import Java.util.Scanner;
public class TWoP {
public static int er (int a[],int start,int stop,int key)
{
if (start>stop) return-1;
int mid= (start+stop)/2;
if (A[mid]==key) return mid;
if (A[mid]>key) return er (a,start,mid-1,key);
Return ER (a,mid+1,stop,key);
}
public static void Main (string[] args)
{
TODO auto-generated method stubs
Scanner sc=new Scanner (system.in);
int S=sc.nextint ();
int a[]=new int [s];
for (int i=0;i<s;i++)
{
A[i]=sc.nextint ();
}
int Key=sc.nextint ();
Arrays.sort (a);
int X=er (A,0,a.length-1,key);
SYSTEM.OUT.PRINTLN (x);
}
}

Algorithms used in algorithm analysis Class 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.