physics chemistry maths science forums
become expert I help I sign up I login
refer a friend - earn nickels!!   
 advanced
 
Home
Ask & Discuss Questions
Study Material
Experts Zone
Hang Out!

Ask & Discuss Questions with Community & Experts

Moderation Team
 90 chars left    advanced
Ask iit jee aieee pet cbse icse state board community Community Discussion Question: arrays
Forum Index -> Computer Science like the article? email it to a friend.  
Author Message
v_gurucharan (283)

Blazing goIITian

Olaaa!! Perrrfect answer. 47  [71 rates]

v_gurucharan's Avatar

total posts: 459    
offline Offline
 pls. post working examples of all the three sorts in arrays (in this thread).
    
v_gurucharan (283)

Blazing goIITian

Olaaa!! Perrrfect answer. 47  [71 rates]

v_gurucharan's Avatar

total posts: 459    
offline Offline
1 more prog......
of

swapping using arrays

Stay Hungry. Stay Foolish.
 this reply: 0 points  (with Olaaa!! Perrrfect answer.   in 0 votes )   [?]
 
You have to be logged on to rate
  
joyfrancis (1504)

Blazing goIITian

Olaaa!! Perrrfect answer. 236  [398 rates]

joyfrancis's Avatar

total posts: 1801    
offline Offline
swaping alternative positions
 
 
for(int i=0;i<n;i+=2)
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
 
This will turn 1,2,3,4,5,6 into 2,1,4,3,6,5

There is no better feeling in this world than being a winner!
 this reply: 5 points  (with Olaaa!! Perrrfect answer.   in 1 votes )   [?]
 
You have to be logged on to rate
  
LAMPARD (1142)

Blazing goIITian

Olaaa!! Perrrfect answer. 190  [286 rates]

LAMPARD's Avatar

total posts: 777    
offline Offline
I have included search and sort in this...
void main() {
                clrscr();
                int a1[SIZE] = {5, 6, 78, 23, 12, 7};
                int a2[SIZE] = {100, 55, 3, 6, 43, 2};
                sort(a1);
                sort(a2);
                merge(a1, a2);
}
 
#include<iostream.h>
#include<conio.h>
#include<string.h>
 
void bSearch(char names[10][30], char num[10][30], char q[30]) {
                int found = 0;
                int l = 0, h = 9, m;
                while(l <= h) {
                                m = (l+h)/2;
                                if(strcmp(names[m], q) < 0)
                                                l = m + 1;
                                else if(strcmp(names[m], q) > 0)
                                                h = m - 1;
                                else {
                                                cout<<q<<" : "<<num[m]<<endl;
                                                found = 1;
                                                break;
                                }
                }
                if(!found)
                                cout<<q<<" was not found\n";
}
 
void linearSearch(char names[10][30], char num[10][30], char q[30]) {
                int found = 0;
                for(int i = 0; i < 10; ++i) {
                                if(strcmp(names[i], q) == 0) {
                                                cout<<q<< " : "<<num[i]<<endl;
                                                found = 1;
                                                break;
                                }
                }
                if(!found)
                                cout<<q<<" was not found\n";
}
 
void seqSort(char names[10][30], char num[10][30]) {
                for(int i = 0; i < 10; ++i) {
                                for(int j = 0; j < 10; ++j) {
                                                if(strcmp(names[j], names[i]) > 0) {
                                                                char tmpN[30], tmpNum[30];
                                                                strcpy(tmpN, names[j]);
                                                                strcpy(tmpNum, num[j]);
                                                                strcpy(names[j], names[i]);
                                                                strcpy(num[j], num[i]);
                                                                strcpy(names[i], tmpN);
                                                                strcpy(num[i], tmpNum);
                                                }
                                }
                }
                for(int k = 0; k < 10; ++k)
                                cout<<names[k]<<" : "<<num[k]<<endl;
 
}
 
void bubbleSort(char names[10][30], char num[10][30]) {
                for(int i = 0; i < 10; i++) {
                                for(int j = 0; j < 9-i; ++j) {
                                                if(strcmp(names[j], names[j+1]) > 0) {
                                                                char tmpN[30], tmpNum[30];
                                                                strcpy(tmpN, names[j]);
                                                                strcpy(tmpNum, num[j]);
                                                                strcpy(names[j], names[j+1]);
                                                                strcpy(num[j], num[j+1]);
                                                                strcpy(names[j+1], tmpN);
                                                                strcpy(num[j+1], tmpNum);
                                                }
                                }
                }
                for(int k = 0; k < 10; ++k)
                                cout<<names[k]<<" : "<<num[k]<<endl;
}
 
void selSort(char names[10][30], char num[10][30]) {
                int pos;
                char hold[30], small[30];
                for(int i = 0; i < 10; ++i) {
                                strcpy(small, names[i]);
                                pos = i;
                                for(int j = i+1; j < 10; ++j) {
                                                if(strcmp(names[j], small) < 0) {
                                                                strcpy(small, names[j]);
                                                                pos = j;
                                                }
                                }
                                strcpy(hold, names[i]);
                                strcpy(names[i], names[pos]);
                                strcpy(names[pos], hold);
                               
                                strcpy(hold, num[i]);
                                strcpy(num[i], num[pos]);
                                strcpy(num[pos], hold);
                }
                for(int k = 0; k < 10; ++k)
                                cout<<names[k]<<" : "<<num[k]<<endl;
}
 
void insertionSort(char names[10][30], char num[10][30]) {
                int i, j;
                char buf[30], bufN[30];
                for(i = 1; i < 10; ++i) {
                                strcpy(buf, names[i]);
                                strcpy(bufN, num[i]);
                               
                                j = i - 1;
                               
                                while( strcmp(buf, names[j]) < 0) {
                                                strcpy(names[j+1], names[j]);
                                                strcpy(num[j+1], num[j]);
                                               
                                                j--;
                                               
                                                if(j < 0)
                                                                break;
                                }
                               
                                strcpy(names[j+1], buf);
                                strcpy(num[j+1], bufN);
                }
                for(int k = 0; k < 10; ++k)
                                cout<<names[k]<<" : "<<num[k]<<endl;
}
 
void main() {
                clrscr();
                char names[10][30] = { "APython", "RRuby", "JJava", "CPlus", "LISP", "RA", "Terry", "Torvalds", "Raymond", "Jobs"};
                char num[10][30] = { "346542", "432608", "02475", "0659749568", "4369476", "198347", "90576", "5745", "980587", "73465"};
               
                char q[30];
                cout<<"Enter name to search for:";
                cin>>q;
               
                int choice = 0;
                do {
                                cout<<"-------------------------------------------\n";
                                cout<<"1. Sort\n2. Search (Binary)\n3. Search (Linear)\n4. Bubble Sort\n5. Selection sort\n6. Insertion Sort\n7. Exit\n";
                                cin>>choice;
                               
                                switch(choice) {
                                                case 1:seqSort(names, num);
                                                                break;
                                                case 2: bSearch(names, num, q);
                                                                break;
                                                case 3: linearSearch(names, num, q);
                                                                break;
                                                case 4: bubbleSort(names, num);
                                                                break;
                                                case 5: selSort(names, num);
                                                                break;
                                                case 6: insertionSort(names, num);
                                                                break;
                                               
                                }
                                cout<<"-------------------------------------------\n";
 
                } while(choice != 7);
                getch();
}
 

MaNuTd RoXxXx..MaNuTd 2 WiN PrEmIeR LeAgUe ThIs SeAsOn ToO AlOnG WiTh ChAmPiOnS LeAgUe.....HaiL RoNaLdO ...HaiL LaMpArD...
 this reply: 0 points  (with Olaaa!! Perrrfect answer.   in 0 votes )   [?]
 
You have to be logged on to rate
  
joyfrancis (1504)

Blazing goIITian

Olaaa!! Perrrfect answer. 236  [398 rates]

joyfrancis's Avatar

total posts: 1801    
offline Offline
another type of swapping in arrays
 
for(int i=0;i<mid;i++)
{
temp=arr[i];
arr[i]=arr[mid+i];
arr[mid+i]=temp;
}
 
This will turn 1,2,3,4,5,6 into 4,5,6,1,2,3

There is no better feeling in this world than being a winner!
 this reply: 5 points  (with Olaaa!! Perrrfect answer.   in 1 votes )   [?]
 
You have to be logged on to rate
  
joyfrancis (1504)

Blazing goIITian