From 4eb07f898232cdabf6758e4be62a442d9c81c7b1 Mon Sep 17 00:00:00 2001 From: Manisha <76945004+MannyP31@users.noreply.github.com> Date: Fri, 16 Apr 2021 19:37:45 +0530 Subject: [PATCH] added some algorithms to Java and C++ (#200) --- arrays/README.md | 9 ++ arrays/c-or-cpp/array.cpp | 22 +++++ arrays/c-or-cpp/data-before-sort.cpp | 36 +++++++ arrays/c-or-cpp/division.cpp | 21 +++++ arrays/c-or-cpp/even-and-odd.c | 40 ++++++++ arrays/c-or-cpp/finding-large-number.cpp | 20 ++++ arrays/c-or-cpp/variable-declaration.cpp | 26 ++++++ .../java/unique-digits-of-large-number.java | 62 +++++++++++++ sorting/README.md | 6 +- sorting/java/bubble-sort.java | 63 ++++++------- sorting/java/heap-sort.java | 69 ++++++++++++++ sorting/java/insertion-sort.java | 71 ++++++-------- sorting/java/selection-sort.java | 93 ++++++++++--------- 13 files changed, 416 insertions(+), 122 deletions(-) create mode 100644 arrays/c-or-cpp/array.cpp create mode 100644 arrays/c-or-cpp/data-before-sort.cpp create mode 100644 arrays/c-or-cpp/division.cpp create mode 100644 arrays/c-or-cpp/even-and-odd.c create mode 100644 arrays/c-or-cpp/finding-large-number.cpp create mode 100644 arrays/c-or-cpp/variable-declaration.cpp create mode 100644 arrays/java/unique-digits-of-large-number.java create mode 100644 sorting/java/heap-sort.java diff --git a/arrays/README.md b/arrays/README.md index 28138a3f..7be7a00a 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -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) diff --git a/arrays/c-or-cpp/array.cpp b/arrays/c-or-cpp/array.cpp new file mode 100644 index 00000000..2d4b4425 --- /dev/null +++ b/arrays/c-or-cpp/array.cpp @@ -0,0 +1,22 @@ +#include +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; +} diff --git a/arrays/c-or-cpp/data-before-sort.cpp b/arrays/c-or-cpp/data-before-sort.cpp new file mode 100644 index 00000000..b90a5092 --- /dev/null +++ b/arrays/c-or-cpp/data-before-sort.cpp @@ -0,0 +1,36 @@ +#include +#include + + 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+1]) + { + temp=a[j]; + a[j]=a[j+1]; + a[j+1]=temp; + } + } + } + cout<<"\nData after sorting: "; + for(j=0;j<10;j++) + { + cout< +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 =" < + +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; +} diff --git a/arrays/c-or-cpp/variable-declaration.cpp b/arrays/c-or-cpp/variable-declaration.cpp new file mode 100644 index 00000000..63667a9a --- /dev/null +++ b/arrays/c-or-cpp/variable-declaration.cpp @@ -0,0 +1,26 @@ +#include +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; +} diff --git a/arrays/java/unique-digits-of-large-number.java b/arrays/java/unique-digits-of-large-number.java new file mode 100644 index 00000000..190a77ea --- /dev/null +++ b/arrays/java/unique-digits-of-large-number.java @@ -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;i1) + { + for(i=0;ic) + c=p.charAt(i); + } + s=""; + s=s+c; + c--; + for(d=c;d>='0';d--) + { + for(i=0;i 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 nums[i]) { + temp = nums[j]; + nums[j] = nums[i]; + nums[i] = temp; + } + } + } + System.out.println(Arrays.toString(nums)); + } +} \ No newline at end of file diff --git a/sorting/java/heap-sort.java b/sorting/java/heap-sort.java new file mode 100644 index 00000000..63f52a19 --- /dev/null +++ b/sorting/java/heap-sort.java @@ -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= 0 && arr[j] > key) { - arr[j + 1] = arr[j]; - j = j - 1; - } - arr[j + 1] = key; - } - } - - /* 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] + " "); - - System.out.println(); - } - - // Driver method - public static void main(String args[]) - { - int arr[] = { 12, 11, 13, 5, 6 }; - - InsertionSort ob = new InsertionSort(); - ob.sort(arr); - - printArray(arr); - } -} \ No newline at end of file + +import java.util.Arrays; + +public class InsertionSort { + static int[] nums = { 12, 43, 2, 4, 1, 0, -23, -93 }; + + public static void main(String[] args) { + System.out.println("Before sorting " + Arrays.toString(nums)); + sort(nums); + } + + public static void sort(int[] nums) { + int i = 1; + while (i < nums.length) { + int temp, j; + temp = nums[i]; + j = i; + + 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)); + + } +} \ No newline at end of file diff --git a/sorting/java/selection-sort.java b/sorting/java/selection-sort.java index 64e19d51..bfe4ba80 100644 --- a/sorting/java/selection-sort.java +++ b/sorting/java/selection-sort.java @@ -1,43 +1,50 @@ -// Java program for implementation of Selection Sort -class SelectionSort -{ - void sort(int arr[]) - { - int n = arr.length; - - // One by one move boundary of unsorted subarray - for (int i = 0; i < n-1; 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; - } - } - - // Prints the array - void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i