[Solved]An Access Violation in merge-sort

Well, in my long (not yet) and hard (already) journey to become a great alghoritm guru, I met an obstacle, obstacle which on paper is logical, and easy to find... but not for me :(

Well, after compiling (in Dev-C++) and running this program, I got error "An Access Violation (Segmentation Fault) raised in your program". I found that it probably means, that I have used same space more than one time, or I have some unexisting item. I found in web already written Merge-Sort in C++ but after comparing them I couldn`t find error (rewriting something what other people wrote is not an option)

I tried debugging, and finding where exactly the error occurs, but program was caught in infinite loop between these two lines of code
1
2
MergeSort(iTablicaA, iPo, iSr);
MergeSort(iTablicaA, iSr+1, iKo);

well.. I think something is wrong with "iTablicaA"


Well then, here`s the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <conio.h>

int Merge(int iTablicaA[], int iPo, int iSr, int iKo)
{

const int iDlug1=(iSr-iPo+1);
const int iDlug2=(iKo-iSr);
int iTablicaB[iDlug1];
int iTablicaC[iDlug2];

for(int i=0;i<=iDlug1;i++)
	{int u=iPo+i-1;
  	iTablicaB[i]=iTablicaA[u];
	}
for(int i=0;i<=iDlug2;i++)
	{int u=iSr+i-1;
  	iTablicaC[i]=iTablicaA[u];}


int i=0;
int j=0;
for(int v=iPo;i<=iKo;v++)
{
        if(iTablicaB[i]<=iTablicaC[j])
	{
		iTablicaA[v]=iTablicaB[i];
 		i++;
		}
	if(iTablicaC[j]<=iTablicaB[i])
	
		iTablicaA[v]=iTablicaC[j];
		j++;
		}
      }
return 0;					 					
}

int MergeSort(int iTablicaA[] ,int iPo, int iKo)
{
 if(iPo < iKo)
 	{
	int iSr=(iKo-iPo)/2;
	MergeSort(iTablicaA, iPo, iSr);
	MergeSort(iTablicaA, iSr+1, iKo);
	Merge(iTablicaA, iPo, iSr, iKo);
	 	   
	}
return 0;
}

int main()
{
int iPo;
int iKo;

const int iDlugosc=4;
int iTablicaA[iDlugosc];


std::cout<<"Podaj dane JO!"<<std::endl;
for(int i=0;i<iDlugosc;i++)
std::cin>>iTablicaA[i];

MergeSort(iTablicaA, iPo=0, iKo=iDlugosc-1);

for(int i=0;i<iDlugosc;i++)
std::cout<<iTablicaA[i]<<std::endl;

getch();
return 0;
 	
}


Unfortunately, even if you solve this error, you aren`t going to get 4 hours of my life, which I have lost to it. But maybe, tommorow you will find 5 cents coin for better luck!
Last edited on
Line 43: int iSr=(iKo+iPo)/2;
Ah... Ah... .... I think i should go outside ;P

Thank you.
Topic archived. No new replies allowed.