import java.util.Arrays; public class SortingDemo { public static void main(String... args){ int a[]={2,1,3,5,4,8,7,9,6}; System.out.println("Before Sorting :"+ Arrays.toString(a)); insertionSort(a); System.out.println("After Sorting :"+ Arrays.toString(a)); } public static void selectionSort(int... arr){ for(int i=0;i<arr.length-1;i++){ for(int j=i+1;j<arr.length;j++){ if(arr[i]>arr[j]){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } } public static void bubbleSort(int... a){ for(int i=0;i<a.length;i++){ for(int j=0;j<a.length-i-1;j++){ if(a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } public static void insertionSort(int... arr){ for(int i=1;i<arr.length;i++){ int j=i-1; int temp=arr[i]; while(j>=0 && temp<arr[j]){ arr[j+1]=arr[j]; j--; } arr[j+1]=temp; } } }
Friday, April 29, 2016
Selection, Bubble, Insertion Sort in Java
Thursday, April 28, 2016
Selection Sort
#include<stdio.h>
#include<conio.h>
void showElements(int arr[],int n){
int i;
if(n==0)
printf("[]");
else if(n>0)
printf("[");
for(i=0;i<n;i++){
if(i==n-1){
printf("%d",arr[i]);
printf("]");
}else
printf("%d,",arr[i]);
}
}
void sortData(int arr[],int n){
int i,j,temp;
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
int getLength(int arr[]){
int i=0;
while(arr[i]!='\0'){
i++;
}
return i;
}
void main(){
int arr[]={2,1,3,4,9,8,7,6,5};
int i,j,len;
clrscr();
len=getLength(arr);
printf("\n Before Sorting Array Elements \n");
showElements(arr,len);
sortData(arr,len);
printf("\n After Sorting Array Elements \n");
showElements(arr,len);
getch();
}
Friday, April 22, 2016
Floating point error in c - struct
//Link float error solution
#include<stdio.h>
#include<conio.h>
struct Employee{
int empno;
char name[50];
float salary;
};
void floatError(float *a){
float b=*a;
floatError(&b);
}
void showDetails(struct Employee *emp){
printf("\n %d %s %f",emp->empno,emp->name,emp->salary);
}
void main(){
struct Employee e1;
struct Employee e2;
clrscr();
e1.empno=1001;
strcpy(e1.name,"Krishna G");
e1.salary=29999.879;
e2.empno=1002;
strcpy(e2.name,"Nanda Gopal Krishna");
e2.salary=59999.987;
showDetails(&e1);
showDetails(&e2);
getch();
}
Stack in C - using array implementation
//Stack implementation using arrays
#include<stdio.h>
#include<conio.h>
int max=10;
int stack[10];
int top=-1;
int i;
void push(int ele){
if(top<max){
stack[++top]=ele;
printf("\n The element is added to stack %d",ele);
}else{
printf("\n Stack is overflow");
}
}
void pop(){
if(top==-1){
printf("\n Stack is underflow");
}else{
printf("\n Element %d is removed ",stack[top]);
top--;
}
}
void display(){
if(top==-1){
printf("\n Stack is empty");
}else{
printf("[");
for(i=0;i<=top;i++){
printf(" %d ",stack[i]);
if(i<top)
printf(",");
}
printf("]");
}
}
void main(){
int ch,ele;
int choice=1;
clrscr();
while(choice){
printf("\n1.PUSH 2.POP 3.DISPLAY\n ");
scanf("%d",&ch);
switch(ch){
case 1:printf("\n Enter the element to push \n");
scanf("%d",&ele);
push(ele);
break;
case 2:pop();
break;
case 3:display();
break;
default: printf("\n Enter valid number (1 to 3)\n");
}
printf("\n Do you want to continue (yes = 1 no = 0) \n");
scanf("%d",&choice);
}
}
Subscribe to:
Posts (Atom)
Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0...
-
Installation of MongoDB The executable file needs to be downloaded from: https://www.mongodb.com/download-center?jmp=nav#communit...
-
Form to add multiple rows: <! DOCTYPE html> <%@ page language = "java" contentType = "text/html; cha...