Monday, February 23, 2015

How do you sort 1 million positive int values (assuming they do not repeat) present in a file (integers.txt) using  limited resources (not more than 2 MB or RAM) and creating only 1 more file (containing the sorted ints) ?
/*Gavin SATHAN*/


import java.io.*;
import java.util.*;

public class Super_Sorting {
public static void main(String[] args) {

int step=1024*1024;

int count=0;

Formatter f=null;

long startTime, endTime,duration;

//This block determines the number of lines in the input file
int numLines=0;
Scanner counterSc=null;
try{counterSc=new Scanner(new File("integers.txt"));}
catch(FileNotFoundException fnfe){ }
while(counterSc.hasNext()){
counterSc.next();
numLines++;
}
counterSc.close();

startTime=System.currentTimeMillis();

//For each value of each line, the appropriate value in the bitset is enabled.
try { f=new Formatter("integers_sorted.txt");}
catch (FileNotFoundException e) {e.printStackTrace();}

long start=0;
long end;
end=step;
int pass=0;
while (start
{
BitSet B=new BitSet(step);The size of the BITSET takes only 1 million bits
B.clear();
start=step*pass;
end=start+step;

System.out.println("Pass Number is "+pass+"\n\t => Filtering values between "+ start+ " and "+ end);
Scanner Sc=null;

try{ Sc=new Scanner(new File("integers.txt")); }
catch(FileNotFoundException fnfe){ }

while(Sc.hasNext()){
int val=Integer.parseInt(Sc.nextLine());
if ((val>=start)&&(val B.set(val-(int)start);
}
}//while
Sc.close();

for (int i = 0; i

if (B.get(i)){
int v=(int)start+i;
System.out.println(v);
f.format("%s\n", v);
count++;
}
}//for
start=end;
end+=step;
pass++;
}//for

endTime=System.currentTimeMillis();
f.close();

//This line determines the duration of the algo
duration=(endTime-startTime)/1000;

System.out.println("Number of lines = "+numLines+"\nNumber of integers sorted="+count+"\nSorting Took "+duration+"seconds");
}//main
}


Friday, February 20, 2015

Delete Recursively from a directory!




/*
Gavin SATHAN
20th February 2015
*/

#include
#include
#include
#include
#include
void DELETE_FOLDER_RECURSIVELY(char * path_name);
void main(){

mkdir("./DEL",0777);
mkdir("./DEL/F1",0777);
mkdir("./DEL/F2",0777);
mkdir("./DEL/F3",0777);
mkdir("./DEL/F3/F3_1",0777);
mkdir("./DEL/F3/F3_2",0777);

int fd=open("./DEL/F2/file1.txt",O_RDONLY|O_CREAT,0777);
close(fd);

fd=open("./DEL/F3/F3_1/file2.txt",O_RDONLY|O_CREAT,0777);
close(fd);

fd=open("./DEL/F3/F3_1/file3.txt",O_RDONLY|O_CREAT,0777);
close(fd);

fd=open("./DEL/F3/F3_2/file4.txt",O_RDONLY|O_CREAT,0777);
close(fd);

fd=open("./DEL/F3/file5.txt",O_RDONLY|O_CREAT,0777);
close(fd);

fd=open("./DEL/file5.txt",O_RDONLY|O_CREAT,0777);
close(fd);


DELETE_FOLDER_RECURSIVELY("./DEL");

}//main


void DELETE_FOLDER_RECURSIVELY(char * path_name){

printf("\n\nInstructed to delete %s", path_name);

DIR * directory_ptr=opendir(path_name);
struct dirent * dp=NULL;
char current[1024]=".";
char parent[1024]="..";
char full_path[1024];




printf("%s",current);
while((dp=readdir(directory_ptr))!=NULL)
{
char arg1[1024];

strcpy(full_path,path_name);
strcat(full_path,"/");
strcpy(arg1,dp->d_name);

if ((strcmp(arg1,current)!=0)&&(strcmp(arg1,parent)!=0)){

strcat(full_path,arg1);

if(dp->d_type!=DT_DIR){

printf("\n\tGot a file, name is %s\n",full_path);

int success=unlink(full_path);

if (success==0){
printf("\t\tDeletion Successful!!");
}
else{
printf("\t\tDeletion Failed!!");
}
}//if DT_DIR

else{
printf("\n\tGot a Directory, name is %s\n",full_path);
DELETE_FOLDER_RECURSIVELY(full_path);
}
}//if
}//while
printf("\nThe Directory %s is now Empty.... \n\tProceeding with the Delete\n\n",path_name);
rmdir(path_name);
}//fn


Thursday, February 12, 2015

Mindset!


This sums it all!!!