added some algorithms to Java and C++ (#200)

pull/216/head
Manisha 2021-04-16 19:37:45 +05:30 committed by GitHub
parent efb642880d
commit 4eb07f8982
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 416 additions and 122 deletions

View File

@ -1,3 +1,5 @@
# Algorithms related to arrays
### C or C++
@ -8,6 +10,12 @@
4. [Shift Negatives in Array](c-or-cpp/shift-negatives.cpp)
5. [Maximum Subarray Sum](c-or-cpp/max-subarray-sum.cpp)
6. [Unique Elements in an Array](c-or-cpp/unique-elements-in-an-array.c)
7. [Sorting arrays](c-or-cpp/array.cpp)
8. [Division of no.](c-or-cpp/division.cpp)
9. [Finding large no.](c-or-cpp/finding-large-number.cpp)
10. [Variable declaration](c-or-cpp/variable-declaration.cpp)
11. [Data before and after sorting](c-or-cpp/data-before-sort.cpp)
12. [Even and odd no. ](c-or-cpp/even-and-odd.c)
### Python
@ -22,3 +30,4 @@
1. [Counting Inversions](java/count-inversions.java)
2. [Kadane's Algorithm](java/Kadanes_Algorithm.java)
2. [Left Rotation of Array](java/left_rotation.java)
3. [Finding unique digits of large number](java/unique-digits-of-large-number.java)

View File

@ -0,0 +1,22 @@
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;
// store input from user to array
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";
// print array elements
for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}
return 0;
}

View File

@ -0,0 +1,36 @@
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
getch();
}

View File

@ -0,0 +1,21 @@
#include <iostream>
using namespace std;
int main() {
int divisor, dividend,quotient,remainder;
cout << "Enter dividend:" ;
cin >> dividend;
cout << "Enter divisor:";
cin >> divisor;
quotient =dividend/divisor;
remainder=dividend%divisor;
cout << "Quotien =" <<quotient << endl;
cout << "remainder=" <<remainder ;
return 0;
}

View File

@ -0,0 +1,40 @@
#include <stdio.h>
int main()
{
int n;
printf("Enter n : "); //n is number of elements in the array
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int arr_odd[n], arr_even[n]; //arr_odd[] for odd elements and arr_even[] for even elements
int o = 0, e = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 != 0)
{
arr_odd[o] = arr[i]; //Putting odd numbers in arr_odd[] array.
o++;
}
else if (arr[i] % 2 == 0)
{
arr_even[e] = arr[i]; //Putting even numbers in arr_even[] array.
e++;
}
}
printf("Even array : [ ");
for (int i = 0; i < e; i++)
{
printf("%d ", arr_even[i]);
}
printf("]\nOdd array : [ ");
for (int i = 0; i < o; i++)
{
printf("%d ", arr_odd[i]);
}
printf("]");
return 0;
}

View File

@ -0,0 +1,20 @@
#include <stdio.h>
int main() {
int a,b,c;
/* Input 3 numbers from user*/
printf("Enter three numbers: ");
scanf("%d%d%d", &a ,&b ,&c);
if (a > b && a > c)
printf("%d is the largest. ", a);
else if (b > a && b > c)
printf("%d is the largest. ", b);
else if (c > a && c > b)
printf("%d is the largest. ", c);
else
printf("Values are not unique");
return 0;
}

View File

@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
// Variable declaration:
extern int a,b;
extern int c;
extern float f;
int main(){
//Variable defination:
int a,b;
int c;
float f;
//actual initialization
a=10;
b=20;
c= a + b;
cout << c << endl ;
f=70.0/3.0;
cout << f << endl;
return 0;
}

View File

@ -0,0 +1,62 @@
import java.util.*;
public class UniqueDigitsOfLargeNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int l,i,j,l1=0;
String s,p="";
char c,d;
System.out.println("Enter a large number");
s=sc.nextLine();
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
for(j=0;j<l1;j++)
{
if(p.charAt(j)==c)
break;
}
if(j==l1)
{
p=p+c;
l1=p.length();
}
}
System.out.print("The unique digits present in "+Long.parseLong(s)+" are ");
if(l1>1)
{
for(i=0;i<l1-1;i++)
{
System.out.print(p.charAt(i)+", ");
}
System.out.println("and "+p.charAt(l1-1)+".");
c=p.charAt(0);
for(i=0;i<l1;i++)
{
if(p.charAt(i)>c)
c=p.charAt(i);
}
s="";
s=s+c;
c--;
for(d=c;d>='0';d--)
{
for(i=0;i<l1;i++)
{
if(p.charAt(i)==d)
s=s+d;
}
}
System.out.println("The largest number possible out of these unique digits is "+Long.parseLong(s));
}
else
{
System.out.println(p.charAt(0)+".");
System.out.println("The largest number possible out of these unique digits is "+Integer.parseInt(p));
}
}
}

View File

@ -24,13 +24,17 @@
### Java
1. [Bubble Sort](java/bubble-sort.java)
2. [Insertion Sort](java/insertion-sort.java)
3. [Selection Sort](java/selection-sort.java)
4. [Merge Sort](java/merge-sort.java)
5. [Quick Sort](java/quick-sort.java)
6. [Counting Sort](java/counting-sort.java)
7. [Bubble Sort algo](java/bubble-sort.java)
8. [Heap Sort Algo](java/heap-sort.java)
9. [Insertion Sort algo](java/insertion-sort.java)
10. [Selection Sort algo](java/selection-sort.java)
### JavaScript
1. [Bubble Sort](js/bubble-sort.js)

View File

@ -1,36 +1,27 @@
// Java program for implementation of Bubble Sort
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[j]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
import java.util.Arrays;
// Driver method to test above
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
int arr[] = {64, 34, 25, 12, 22, 11, 90};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
public class BubbleSort {
static int temp = 0;
static int[] nums = { 13, 34, 12, 45, 56, 32, 20 };
public static void main(String[] args) {
System.out.println("Before Sorting : "+Arrays.toString(nums));
sort();
}
//create method sorting array
public static void sort() {
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] > nums[i]) {
temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
}
}
System.out.println(Arrays.toString(nums));
}
}

View File

@ -0,0 +1,69 @@
import java.util.Scanner;
public class HeapSort
{
public void sort(int array[])
{
int n = array.length;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(array, n, i);
for (int i = n-1; i >= 0; i--)
{
int temp = array[0];
array[0] = array[i];
array[i] = temp;
heapify(array, i, 0);
}
}
void heapify(int array[], int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && array[l] > array[largest])
largest = l;
if (r < n && array[r] > array[largest])
largest = r;
if (largest != i)
{
int swap = array[i];
array[i] = array[largest];
array[largest] = swap;
heapify(array, n, largest);
}
}
public static void main(String args[])
{
System.out.println("enter the array size");
Scanner inputobj = new Scanner(System.in);
int s = inputobj.nextInt();
int array[] = new int[s];
System.out.println("enter " + s + "elements of array");
for(int k=0;k<s;k++)
{
int e = inputobj.nextInt();
array[k] = e;
}
HeapSort ob = new HeapSort();
ob.sort(array);
System.out.println("Sorted array is");
for (int i=0; i<s; ++i)
System.out.print(array[i]+" ");
System.out.println();
}
}

View File

@ -1,42 +1,29 @@
// Java program for implementation of Insertion Sort
class InsertionSort {
/*Function to sort array using insertion sort*/
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
import java.util.Arrays;
/* A utility function to print array of size n*/
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
public class InsertionSort {
static int[] nums = { 12, 43, 2, 4, 1, 0, -23, -93 };
System.out.println();
}
public static void main(String[] args) {
System.out.println("Before sorting " + Arrays.toString(nums));
sort(nums);
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
public static void sort(int[] nums) {
int i = 1;
while (i < nums.length) {
int temp, j;
temp = nums[i];
j = i;
InsertionSort ob = new InsertionSort();
ob.sort(arr);
while (j > 0 && nums[j - 1] > temp) {
nums[j] = nums[j - 1];
j--;
}
nums[j] = temp;
i++;
}
System.out.println("After Sorting " + Arrays.toString(nums));
printArray(arr);
}
}
}

View File

@ -1,43 +1,50 @@
// Java program for implementation of Selection Sort
class SelectionSort
class Selection
{
void sort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
int minIndex(int Array[] , int start, int end)
{
int minIndex = start;
for (int i = start+1; i < end; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
if ( Array[i] < Array[minIndex] )
{
minIndex = i;
}
}
return minIndex;
}
// Prints the array
void printArray(int arr[])
int[] sorting(int Array[],int length)
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
// Driver code to test above
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
for (int i = 0; i < length-1; i++)
{
int minI = minIndex(Array, i, length);
int temp = Array[minI];
Array[minI] = Array[i];
Array[i] = temp;
}
return Array;
}
}
/**
* SelectionSort
*/
public class SelectionSort {
public static void main(String[] args) {
int Array[] = {1,2,3,4,5,6,7,8,9};
Selection s1 = new Selection();
long startTime = System.nanoTime();
int sortedArray[] = s1.sorting(Array, 9);
long endTime = System.nanoTime();
for (int i = 0; i < 9; i++) {
System.out.println(sortedArray[i]);
}
System.out.println("Total Time in Neno Second: "+ (endTime-startTime));
}
}