Skip to content

Instantly share code, notes, and snippets.

View shankernaik's full-sized avatar
👨‍🔬

Shanker Naik shankernaik

👨‍🔬
View GitHub Profile
@shankernaik
shankernaik / Insertion Sort
Created April 22, 2015 14:22
Insertion sort in java
class test{
static void printArray(int arr[])
{
int i;
for (i=0; i < arr.length; i++)
System.out.print(" "+arr[i]);
System.out.println();
@shankernaik
shankernaik / gist:8492251
Last active January 3, 2016 16:49
Finding loop in Graph given adjacent matrix
#include <stdio.h>
#define N_NODES 4
/* A B C D
A---B A { 0, 1, 0, 0 }
/ \ B { 1, 0, 1, 1 }
/ \ C { 0, 1, 0, 1 }
/ \ D { 0, 1, 1, 0 }
D-------C
@shankernaik
shankernaik / gist:8488012
Last active January 15, 2021 13:04
Swap Tree code
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
@shankernaik
shankernaik / gist:8332654
Created January 9, 2014 11:17
For a given n, count the total number of zeros in the decimal representation of the positive integers less than or equal to n. For instance, there are 11 zeros in the decimal representations of the numbers less than or equal to 100, in the numbers 10, 20, 30, 40, 50, 60, 70, 80, 90, and twice in the number 100.
n=100
count=0
for i in range(10,n+1):
a=str(i)
count+=a.count('0')
print count
@shankernaik
shankernaik / gist:8185256
Created December 30, 2013 17:42
Heap and heap sort
public class HeapSort {
public static final int max = 11;
public static void main(String[] args){
int[] toSortArray = new int[max];
//Required Things
//End
toSortArray[0] = 0;
for(int i = 1; i < max; i++){
toSortArray[i] = (int) (Math.random()*100);
toSortArray[0]++; //holds the number of values in the array;
Factoring Methods
Algorithm description
1. Establish n the number whose prime factors are sought.
2. Compute the remainder r and quotient q for the first prime nxtprime =2
3. While n is not prime do (not able to understand)
a. if nxtprime is an exact divisor of n then
(a.1) save nxtprime as a factor f,
(a.2) reduce n by nxtprime.
else
@shankernaik
shankernaik / gist:8169606
Created December 29, 2013 11:48
Factoring Methods
Factoring Methods
The smallest divisor of an Integer
Algorithm description
1. Establish n the integer whose smallest divisor is required.
2. If n is not odd, then
return 2 as the smallest divisor
else
@shankernaik
shankernaik / collections
Created December 28, 2013 10:03
Collections in Java
package test;
import java.util.*;
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
@shankernaik
shankernaik / quick sort
Created December 28, 2013 07:50
Sorting by Partitioning (quick sort / hoare's method) - Sorting by partitioning is a simple sorting algorithm that works by choosing a certain element, called the pivot, and then dividing the list into two parts, those less than the pivot and those greater than or equal to the pivot. Each list is then recursively sorted. For arrays on typical mo…
Algorithm description
Establish array a[1...n] to be sorted.
Place upper and lower limits for array on the stack and initialize pointer to top of stack.
While stack is not empty do,
remove upper and lower limits of array segment from top of Stack.
While current segment not reduced to size 1 do,
(b.1) Select the middle element of array segment from stack.
(b.2) Partition the current segment into two with respect to the current